journal.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Write ahead logging implementation copyright Chris Mason 2000
  4. *
  5. * The background commits make this code very interrelated, and
  6. * overly complex. I need to rethink things a bit....The major players:
  7. *
  8. * journal_begin -- call with the number of blocks you expect to log.
  9. * If the current transaction is too
  10. * old, it will block until the current transaction is
  11. * finished, and then start a new one.
  12. * Usually, your transaction will get joined in with
  13. * previous ones for speed.
  14. *
  15. * journal_join -- same as journal_begin, but won't block on the current
  16. * transaction regardless of age. Don't ever call
  17. * this. Ever. There are only two places it should be
  18. * called from, and they are both inside this file.
  19. *
  20. * journal_mark_dirty -- adds blocks into this transaction. clears any flags
  21. * that might make them get sent to disk
  22. * and then marks them BH_JDirty. Puts the buffer head
  23. * into the current transaction hash.
  24. *
  25. * journal_end -- if the current transaction is batchable, it does nothing
  26. * otherwise, it could do an async/synchronous commit, or
  27. * a full flush of all log and real blocks in the
  28. * transaction.
  29. *
  30. * flush_old_commits -- if the current transaction is too old, it is ended and
  31. * commit blocks are sent to disk. Forces commit blocks
  32. * to disk for all backgrounded commits that have been
  33. * around too long.
  34. * -- Note, if you call this as an immediate flush from
  35. * from within kupdate, it will ignore the immediate flag
  36. */
  37. #include <linux/time.h>
  38. #include <linux/semaphore.h>
  39. #include <linux/vmalloc.h>
  40. #include "reiserfs.h"
  41. #include <linux/kernel.h>
  42. #include <linux/errno.h>
  43. #include <linux/fcntl.h>
  44. #include <linux/stat.h>
  45. #include <linux/string.h>
  46. #include <linux/buffer_head.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/writeback.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/backing-dev.h>
  51. #include <linux/uaccess.h>
  52. #include <linux/slab.h>
  53. /* gets a struct reiserfs_journal_list * from a list head */
  54. #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
  55. j_list))
  56. #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
  57. j_working_list))
  58. /* must be correct to keep the desc and commit structs at 4k */
  59. #define JOURNAL_TRANS_HALF 1018
  60. #define BUFNR 64 /*read ahead */
  61. /* cnode stat bits. Move these into reiserfs_fs.h */
  62. /* this block was freed, and can't be written. */
  63. #define BLOCK_FREED 2
  64. /* this block was freed during this transaction, and can't be written */
  65. #define BLOCK_FREED_HOLDER 3
  66. /* used in flush_journal_list */
  67. #define BLOCK_NEEDS_FLUSH 4
  68. #define BLOCK_DIRTIED 5
  69. /* journal list state bits */
  70. #define LIST_TOUCHED 1
  71. #define LIST_DIRTY 2
  72. #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
  73. /* flags for do_journal_end */
  74. #define FLUSH_ALL 1 /* flush commit and real blocks */
  75. #define COMMIT_NOW 2 /* end and commit this transaction */
  76. #define WAIT 4 /* wait for the log blocks to hit the disk */
  77. static int do_journal_end(struct reiserfs_transaction_handle *, int flags);
  78. static int flush_journal_list(struct super_block *s,
  79. struct reiserfs_journal_list *jl, int flushall);
  80. static int flush_commit_list(struct super_block *s,
  81. struct reiserfs_journal_list *jl, int flushall);
  82. static int can_dirty(struct reiserfs_journal_cnode *cn);
  83. static int journal_join(struct reiserfs_transaction_handle *th,
  84. struct super_block *sb);
  85. static void release_journal_dev(struct super_block *super,
  86. struct reiserfs_journal *journal);
  87. static int dirty_one_transaction(struct super_block *s,
  88. struct reiserfs_journal_list *jl);
  89. static void flush_async_commits(struct work_struct *work);
  90. static void queue_log_writer(struct super_block *s);
  91. /* values for join in do_journal_begin_r */
  92. enum {
  93. JBEGIN_REG = 0, /* regular journal begin */
  94. /* join the running transaction if at all possible */
  95. JBEGIN_JOIN = 1,
  96. /* called from cleanup code, ignores aborted flag */
  97. JBEGIN_ABORT = 2,
  98. };
  99. static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
  100. struct super_block *sb,
  101. unsigned long nblocks, int join);
  102. static void init_journal_hash(struct super_block *sb)
  103. {
  104. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  105. memset(journal->j_hash_table, 0,
  106. JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
  107. }
  108. /*
  109. * clears BH_Dirty and sticks the buffer on the clean list. Called because
  110. * I can't allow refile_buffer to make schedule happen after I've freed a
  111. * block. Look at remove_from_transaction and journal_mark_freed for
  112. * more details.
  113. */
  114. static int reiserfs_clean_and_file_buffer(struct buffer_head *bh)
  115. {
  116. if (bh) {
  117. clear_buffer_dirty(bh);
  118. clear_buffer_journal_test(bh);
  119. }
  120. return 0;
  121. }
  122. static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block
  123. *sb)
  124. {
  125. struct reiserfs_bitmap_node *bn;
  126. static int id;
  127. bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS);
  128. if (!bn) {
  129. return NULL;
  130. }
  131. bn->data = kzalloc(sb->s_blocksize, GFP_NOFS);
  132. if (!bn->data) {
  133. kfree(bn);
  134. return NULL;
  135. }
  136. bn->id = id++;
  137. INIT_LIST_HEAD(&bn->list);
  138. return bn;
  139. }
  140. static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *sb)
  141. {
  142. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  143. struct reiserfs_bitmap_node *bn = NULL;
  144. struct list_head *entry = journal->j_bitmap_nodes.next;
  145. journal->j_used_bitmap_nodes++;
  146. repeat:
  147. if (entry != &journal->j_bitmap_nodes) {
  148. bn = list_entry(entry, struct reiserfs_bitmap_node, list);
  149. list_del(entry);
  150. memset(bn->data, 0, sb->s_blocksize);
  151. journal->j_free_bitmap_nodes--;
  152. return bn;
  153. }
  154. bn = allocate_bitmap_node(sb);
  155. if (!bn) {
  156. yield();
  157. goto repeat;
  158. }
  159. return bn;
  160. }
  161. static inline void free_bitmap_node(struct super_block *sb,
  162. struct reiserfs_bitmap_node *bn)
  163. {
  164. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  165. journal->j_used_bitmap_nodes--;
  166. if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
  167. kfree(bn->data);
  168. kfree(bn);
  169. } else {
  170. list_add(&bn->list, &journal->j_bitmap_nodes);
  171. journal->j_free_bitmap_nodes++;
  172. }
  173. }
  174. static void allocate_bitmap_nodes(struct super_block *sb)
  175. {
  176. int i;
  177. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  178. struct reiserfs_bitmap_node *bn = NULL;
  179. for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) {
  180. bn = allocate_bitmap_node(sb);
  181. if (bn) {
  182. list_add(&bn->list, &journal->j_bitmap_nodes);
  183. journal->j_free_bitmap_nodes++;
  184. } else {
  185. /* this is ok, we'll try again when more are needed */
  186. break;
  187. }
  188. }
  189. }
  190. static int set_bit_in_list_bitmap(struct super_block *sb,
  191. b_blocknr_t block,
  192. struct reiserfs_list_bitmap *jb)
  193. {
  194. unsigned int bmap_nr = block / (sb->s_blocksize << 3);
  195. unsigned int bit_nr = block % (sb->s_blocksize << 3);
  196. if (!jb->bitmaps[bmap_nr]) {
  197. jb->bitmaps[bmap_nr] = get_bitmap_node(sb);
  198. }
  199. set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data);
  200. return 0;
  201. }
  202. static void cleanup_bitmap_list(struct super_block *sb,
  203. struct reiserfs_list_bitmap *jb)
  204. {
  205. int i;
  206. if (jb->bitmaps == NULL)
  207. return;
  208. for (i = 0; i < reiserfs_bmap_count(sb); i++) {
  209. if (jb->bitmaps[i]) {
  210. free_bitmap_node(sb, jb->bitmaps[i]);
  211. jb->bitmaps[i] = NULL;
  212. }
  213. }
  214. }
  215. /*
  216. * only call this on FS unmount.
  217. */
  218. static int free_list_bitmaps(struct super_block *sb,
  219. struct reiserfs_list_bitmap *jb_array)
  220. {
  221. int i;
  222. struct reiserfs_list_bitmap *jb;
  223. for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
  224. jb = jb_array + i;
  225. jb->journal_list = NULL;
  226. cleanup_bitmap_list(sb, jb);
  227. vfree(jb->bitmaps);
  228. jb->bitmaps = NULL;
  229. }
  230. return 0;
  231. }
  232. static int free_bitmap_nodes(struct super_block *sb)
  233. {
  234. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  235. struct list_head *next = journal->j_bitmap_nodes.next;
  236. struct reiserfs_bitmap_node *bn;
  237. while (next != &journal->j_bitmap_nodes) {
  238. bn = list_entry(next, struct reiserfs_bitmap_node, list);
  239. list_del(next);
  240. kfree(bn->data);
  241. kfree(bn);
  242. next = journal->j_bitmap_nodes.next;
  243. journal->j_free_bitmap_nodes--;
  244. }
  245. return 0;
  246. }
  247. /*
  248. * get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
  249. * jb_array is the array to be filled in.
  250. */
  251. int reiserfs_allocate_list_bitmaps(struct super_block *sb,
  252. struct reiserfs_list_bitmap *jb_array,
  253. unsigned int bmap_nr)
  254. {
  255. int i;
  256. int failed = 0;
  257. struct reiserfs_list_bitmap *jb;
  258. int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *);
  259. for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
  260. jb = jb_array + i;
  261. jb->journal_list = NULL;
  262. jb->bitmaps = vzalloc(mem);
  263. if (!jb->bitmaps) {
  264. reiserfs_warning(sb, "clm-2000", "unable to "
  265. "allocate bitmaps for journal lists");
  266. failed = 1;
  267. break;
  268. }
  269. }
  270. if (failed) {
  271. free_list_bitmaps(sb, jb_array);
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. /*
  277. * find an available list bitmap. If you can't find one, flush a commit list
  278. * and try again
  279. */
  280. static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *sb,
  281. struct reiserfs_journal_list
  282. *jl)
  283. {
  284. int i, j;
  285. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  286. struct reiserfs_list_bitmap *jb = NULL;
  287. for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) {
  288. i = journal->j_list_bitmap_index;
  289. journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS;
  290. jb = journal->j_list_bitmap + i;
  291. if (journal->j_list_bitmap[i].journal_list) {
  292. flush_commit_list(sb,
  293. journal->j_list_bitmap[i].
  294. journal_list, 1);
  295. if (!journal->j_list_bitmap[i].journal_list) {
  296. break;
  297. }
  298. } else {
  299. break;
  300. }
  301. }
  302. /* double check to make sure if flushed correctly */
  303. if (jb->journal_list)
  304. return NULL;
  305. jb->journal_list = jl;
  306. return jb;
  307. }
  308. /*
  309. * allocates a new chunk of X nodes, and links them all together as a list.
  310. * Uses the cnode->next and cnode->prev pointers
  311. * returns NULL on failure
  312. */
  313. static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes)
  314. {
  315. struct reiserfs_journal_cnode *head;
  316. int i;
  317. if (num_cnodes <= 0) {
  318. return NULL;
  319. }
  320. head = vzalloc(array_size(num_cnodes,
  321. sizeof(struct reiserfs_journal_cnode)));
  322. if (!head) {
  323. return NULL;
  324. }
  325. head[0].prev = NULL;
  326. head[0].next = head + 1;
  327. for (i = 1; i < num_cnodes; i++) {
  328. head[i].prev = head + (i - 1);
  329. head[i].next = head + (i + 1); /* if last one, overwrite it after the if */
  330. }
  331. head[num_cnodes - 1].next = NULL;
  332. return head;
  333. }
  334. /* pulls a cnode off the free list, or returns NULL on failure */
  335. static struct reiserfs_journal_cnode *get_cnode(struct super_block *sb)
  336. {
  337. struct reiserfs_journal_cnode *cn;
  338. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  339. reiserfs_check_lock_depth(sb, "get_cnode");
  340. if (journal->j_cnode_free <= 0) {
  341. return NULL;
  342. }
  343. journal->j_cnode_used++;
  344. journal->j_cnode_free--;
  345. cn = journal->j_cnode_free_list;
  346. if (!cn) {
  347. return cn;
  348. }
  349. if (cn->next) {
  350. cn->next->prev = NULL;
  351. }
  352. journal->j_cnode_free_list = cn->next;
  353. memset(cn, 0, sizeof(struct reiserfs_journal_cnode));
  354. return cn;
  355. }
  356. /*
  357. * returns a cnode to the free list
  358. */
  359. static void free_cnode(struct super_block *sb,
  360. struct reiserfs_journal_cnode *cn)
  361. {
  362. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  363. reiserfs_check_lock_depth(sb, "free_cnode");
  364. journal->j_cnode_used--;
  365. journal->j_cnode_free++;
  366. /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
  367. cn->next = journal->j_cnode_free_list;
  368. if (journal->j_cnode_free_list) {
  369. journal->j_cnode_free_list->prev = cn;
  370. }
  371. cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */
  372. journal->j_cnode_free_list = cn;
  373. }
  374. static void clear_prepared_bits(struct buffer_head *bh)
  375. {
  376. clear_buffer_journal_prepared(bh);
  377. clear_buffer_journal_restore_dirty(bh);
  378. }
  379. /*
  380. * return a cnode with same dev, block number and size in table,
  381. * or null if not found
  382. */
  383. static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct
  384. super_block
  385. *sb,
  386. struct
  387. reiserfs_journal_cnode
  388. **table,
  389. long bl)
  390. {
  391. struct reiserfs_journal_cnode *cn;
  392. cn = journal_hash(table, sb, bl);
  393. while (cn) {
  394. if (cn->blocknr == bl && cn->sb == sb)
  395. return cn;
  396. cn = cn->hnext;
  397. }
  398. return (struct reiserfs_journal_cnode *)0;
  399. }
  400. /*
  401. * this actually means 'can this block be reallocated yet?'. If you set
  402. * search_all, a block can only be allocated if it is not in the current
  403. * transaction, was not freed by the current transaction, and has no chance
  404. * of ever being overwritten by a replay after crashing.
  405. *
  406. * If you don't set search_all, a block can only be allocated if it is not
  407. * in the current transaction. Since deleting a block removes it from the
  408. * current transaction, this case should never happen. If you don't set
  409. * search_all, make sure you never write the block without logging it.
  410. *
  411. * next_zero_bit is a suggestion about the next block to try for find_forward.
  412. * when bl is rejected because it is set in a journal list bitmap, we search
  413. * for the next zero bit in the bitmap that rejected bl. Then, we return
  414. * that through next_zero_bit for find_forward to try.
  415. *
  416. * Just because we return something in next_zero_bit does not mean we won't
  417. * reject it on the next call to reiserfs_in_journal
  418. */
  419. int reiserfs_in_journal(struct super_block *sb,
  420. unsigned int bmap_nr, int bit_nr, int search_all,
  421. b_blocknr_t * next_zero_bit)
  422. {
  423. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  424. struct reiserfs_journal_cnode *cn;
  425. struct reiserfs_list_bitmap *jb;
  426. int i;
  427. unsigned long bl;
  428. *next_zero_bit = 0; /* always start this at zero. */
  429. PROC_INFO_INC(sb, journal.in_journal);
  430. /*
  431. * If we aren't doing a search_all, this is a metablock, and it
  432. * will be logged before use. if we crash before the transaction
  433. * that freed it commits, this transaction won't have committed
  434. * either, and the block will never be written
  435. */
  436. if (search_all) {
  437. for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
  438. PROC_INFO_INC(sb, journal.in_journal_bitmap);
  439. jb = journal->j_list_bitmap + i;
  440. if (jb->journal_list && jb->bitmaps[bmap_nr] &&
  441. test_bit(bit_nr,
  442. (unsigned long *)jb->bitmaps[bmap_nr]->
  443. data)) {
  444. *next_zero_bit =
  445. find_next_zero_bit((unsigned long *)
  446. (jb->bitmaps[bmap_nr]->
  447. data),
  448. sb->s_blocksize << 3,
  449. bit_nr + 1);
  450. return 1;
  451. }
  452. }
  453. }
  454. bl = bmap_nr * (sb->s_blocksize << 3) + bit_nr;
  455. /* is it in any old transactions? */
  456. if (search_all
  457. && (cn =
  458. get_journal_hash_dev(sb, journal->j_list_hash_table, bl))) {
  459. return 1;
  460. }
  461. /* is it in the current transaction. This should never happen */
  462. if ((cn = get_journal_hash_dev(sb, journal->j_hash_table, bl))) {
  463. BUG();
  464. return 1;
  465. }
  466. PROC_INFO_INC(sb, journal.in_journal_reusable);
  467. /* safe for reuse */
  468. return 0;
  469. }
  470. /* insert cn into table */
  471. static inline void insert_journal_hash(struct reiserfs_journal_cnode **table,
  472. struct reiserfs_journal_cnode *cn)
  473. {
  474. struct reiserfs_journal_cnode *cn_orig;
  475. cn_orig = journal_hash(table, cn->sb, cn->blocknr);
  476. cn->hnext = cn_orig;
  477. cn->hprev = NULL;
  478. if (cn_orig) {
  479. cn_orig->hprev = cn;
  480. }
  481. journal_hash(table, cn->sb, cn->blocknr) = cn;
  482. }
  483. /* lock the current transaction */
  484. static inline void lock_journal(struct super_block *sb)
  485. {
  486. PROC_INFO_INC(sb, journal.lock_journal);
  487. reiserfs_mutex_lock_safe(&SB_JOURNAL(sb)->j_mutex, sb);
  488. }
  489. /* unlock the current transaction */
  490. static inline void unlock_journal(struct super_block *sb)
  491. {
  492. mutex_unlock(&SB_JOURNAL(sb)->j_mutex);
  493. }
  494. static inline void get_journal_list(struct reiserfs_journal_list *jl)
  495. {
  496. jl->j_refcount++;
  497. }
  498. static inline void put_journal_list(struct super_block *s,
  499. struct reiserfs_journal_list *jl)
  500. {
  501. if (jl->j_refcount < 1) {
  502. reiserfs_panic(s, "journal-2", "trans id %u, refcount at %d",
  503. jl->j_trans_id, jl->j_refcount);
  504. }
  505. if (--jl->j_refcount == 0)
  506. kfree(jl);
  507. }
  508. /*
  509. * this used to be much more involved, and I'm keeping it just in case
  510. * things get ugly again. it gets called by flush_commit_list, and
  511. * cleans up any data stored about blocks freed during a transaction.
  512. */
  513. static void cleanup_freed_for_journal_list(struct super_block *sb,
  514. struct reiserfs_journal_list *jl)
  515. {
  516. struct reiserfs_list_bitmap *jb = jl->j_list_bitmap;
  517. if (jb) {
  518. cleanup_bitmap_list(sb, jb);
  519. }
  520. jl->j_list_bitmap->journal_list = NULL;
  521. jl->j_list_bitmap = NULL;
  522. }
  523. static int journal_list_still_alive(struct super_block *s,
  524. unsigned int trans_id)
  525. {
  526. struct reiserfs_journal *journal = SB_JOURNAL(s);
  527. struct list_head *entry = &journal->j_journal_list;
  528. struct reiserfs_journal_list *jl;
  529. if (!list_empty(entry)) {
  530. jl = JOURNAL_LIST_ENTRY(entry->next);
  531. if (jl->j_trans_id <= trans_id) {
  532. return 1;
  533. }
  534. }
  535. return 0;
  536. }
  537. /*
  538. * If page->mapping was null, we failed to truncate this page for
  539. * some reason. Most likely because it was truncated after being
  540. * logged via data=journal.
  541. *
  542. * This does a check to see if the buffer belongs to one of these
  543. * lost pages before doing the final put_bh. If page->mapping was
  544. * null, it tries to free buffers on the page, which should make the
  545. * final put_page drop the page from the lru.
  546. */
  547. static void release_buffer_page(struct buffer_head *bh)
  548. {
  549. struct page *page = bh->b_page;
  550. if (!page->mapping && trylock_page(page)) {
  551. get_page(page);
  552. put_bh(bh);
  553. if (!page->mapping)
  554. try_to_free_buffers(page);
  555. unlock_page(page);
  556. put_page(page);
  557. } else {
  558. put_bh(bh);
  559. }
  560. }
  561. static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
  562. {
  563. if (buffer_journaled(bh)) {
  564. reiserfs_warning(NULL, "clm-2084",
  565. "pinned buffer %lu:%pg sent to disk",
  566. bh->b_blocknr, bh->b_bdev);
  567. }
  568. if (uptodate)
  569. set_buffer_uptodate(bh);
  570. else
  571. clear_buffer_uptodate(bh);
  572. unlock_buffer(bh);
  573. release_buffer_page(bh);
  574. }
  575. static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate)
  576. {
  577. if (uptodate)
  578. set_buffer_uptodate(bh);
  579. else
  580. clear_buffer_uptodate(bh);
  581. unlock_buffer(bh);
  582. put_bh(bh);
  583. }
  584. static void submit_logged_buffer(struct buffer_head *bh)
  585. {
  586. get_bh(bh);
  587. bh->b_end_io = reiserfs_end_buffer_io_sync;
  588. clear_buffer_journal_new(bh);
  589. clear_buffer_dirty(bh);
  590. if (!test_clear_buffer_journal_test(bh))
  591. BUG();
  592. if (!buffer_uptodate(bh))
  593. BUG();
  594. submit_bh(REQ_OP_WRITE, 0, bh);
  595. }
  596. static void submit_ordered_buffer(struct buffer_head *bh)
  597. {
  598. get_bh(bh);
  599. bh->b_end_io = reiserfs_end_ordered_io;
  600. clear_buffer_dirty(bh);
  601. if (!buffer_uptodate(bh))
  602. BUG();
  603. submit_bh(REQ_OP_WRITE, 0, bh);
  604. }
  605. #define CHUNK_SIZE 32
  606. struct buffer_chunk {
  607. struct buffer_head *bh[CHUNK_SIZE];
  608. int nr;
  609. };
  610. static void write_chunk(struct buffer_chunk *chunk)
  611. {
  612. int i;
  613. for (i = 0; i < chunk->nr; i++) {
  614. submit_logged_buffer(chunk->bh[i]);
  615. }
  616. chunk->nr = 0;
  617. }
  618. static void write_ordered_chunk(struct buffer_chunk *chunk)
  619. {
  620. int i;
  621. for (i = 0; i < chunk->nr; i++) {
  622. submit_ordered_buffer(chunk->bh[i]);
  623. }
  624. chunk->nr = 0;
  625. }
  626. static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
  627. spinlock_t * lock, void (fn) (struct buffer_chunk *))
  628. {
  629. int ret = 0;
  630. BUG_ON(chunk->nr >= CHUNK_SIZE);
  631. chunk->bh[chunk->nr++] = bh;
  632. if (chunk->nr >= CHUNK_SIZE) {
  633. ret = 1;
  634. if (lock) {
  635. spin_unlock(lock);
  636. fn(chunk);
  637. spin_lock(lock);
  638. } else {
  639. fn(chunk);
  640. }
  641. }
  642. return ret;
  643. }
  644. static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
  645. static struct reiserfs_jh *alloc_jh(void)
  646. {
  647. struct reiserfs_jh *jh;
  648. while (1) {
  649. jh = kmalloc(sizeof(*jh), GFP_NOFS);
  650. if (jh) {
  651. atomic_inc(&nr_reiserfs_jh);
  652. return jh;
  653. }
  654. yield();
  655. }
  656. }
  657. /*
  658. * we want to free the jh when the buffer has been written
  659. * and waited on
  660. */
  661. void reiserfs_free_jh(struct buffer_head *bh)
  662. {
  663. struct reiserfs_jh *jh;
  664. jh = bh->b_private;
  665. if (jh) {
  666. bh->b_private = NULL;
  667. jh->bh = NULL;
  668. list_del_init(&jh->list);
  669. kfree(jh);
  670. if (atomic_read(&nr_reiserfs_jh) <= 0)
  671. BUG();
  672. atomic_dec(&nr_reiserfs_jh);
  673. put_bh(bh);
  674. }
  675. }
  676. static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
  677. int tail)
  678. {
  679. struct reiserfs_jh *jh;
  680. if (bh->b_private) {
  681. spin_lock(&j->j_dirty_buffers_lock);
  682. if (!bh->b_private) {
  683. spin_unlock(&j->j_dirty_buffers_lock);
  684. goto no_jh;
  685. }
  686. jh = bh->b_private;
  687. list_del_init(&jh->list);
  688. } else {
  689. no_jh:
  690. get_bh(bh);
  691. jh = alloc_jh();
  692. spin_lock(&j->j_dirty_buffers_lock);
  693. /*
  694. * buffer must be locked for __add_jh, should be able to have
  695. * two adds at the same time
  696. */
  697. BUG_ON(bh->b_private);
  698. jh->bh = bh;
  699. bh->b_private = jh;
  700. }
  701. jh->jl = j->j_current_jl;
  702. if (tail)
  703. list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
  704. else {
  705. list_add_tail(&jh->list, &jh->jl->j_bh_list);
  706. }
  707. spin_unlock(&j->j_dirty_buffers_lock);
  708. return 0;
  709. }
  710. int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh)
  711. {
  712. return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
  713. }
  714. int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh)
  715. {
  716. return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
  717. }
  718. #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
  719. static int write_ordered_buffers(spinlock_t * lock,
  720. struct reiserfs_journal *j,
  721. struct reiserfs_journal_list *jl,
  722. struct list_head *list)
  723. {
  724. struct buffer_head *bh;
  725. struct reiserfs_jh *jh;
  726. int ret = j->j_errno;
  727. struct buffer_chunk chunk;
  728. struct list_head tmp;
  729. INIT_LIST_HEAD(&tmp);
  730. chunk.nr = 0;
  731. spin_lock(lock);
  732. while (!list_empty(list)) {
  733. jh = JH_ENTRY(list->next);
  734. bh = jh->bh;
  735. get_bh(bh);
  736. if (!trylock_buffer(bh)) {
  737. if (!buffer_dirty(bh)) {
  738. list_move(&jh->list, &tmp);
  739. goto loop_next;
  740. }
  741. spin_unlock(lock);
  742. if (chunk.nr)
  743. write_ordered_chunk(&chunk);
  744. wait_on_buffer(bh);
  745. cond_resched();
  746. spin_lock(lock);
  747. goto loop_next;
  748. }
  749. /*
  750. * in theory, dirty non-uptodate buffers should never get here,
  751. * but the upper layer io error paths still have a few quirks.
  752. * Handle them here as gracefully as we can
  753. */
  754. if (!buffer_uptodate(bh) && buffer_dirty(bh)) {
  755. clear_buffer_dirty(bh);
  756. ret = -EIO;
  757. }
  758. if (buffer_dirty(bh)) {
  759. list_move(&jh->list, &tmp);
  760. add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
  761. } else {
  762. reiserfs_free_jh(bh);
  763. unlock_buffer(bh);
  764. }
  765. loop_next:
  766. put_bh(bh);
  767. cond_resched_lock(lock);
  768. }
  769. if (chunk.nr) {
  770. spin_unlock(lock);
  771. write_ordered_chunk(&chunk);
  772. spin_lock(lock);
  773. }
  774. while (!list_empty(&tmp)) {
  775. jh = JH_ENTRY(tmp.prev);
  776. bh = jh->bh;
  777. get_bh(bh);
  778. reiserfs_free_jh(bh);
  779. if (buffer_locked(bh)) {
  780. spin_unlock(lock);
  781. wait_on_buffer(bh);
  782. spin_lock(lock);
  783. }
  784. if (!buffer_uptodate(bh)) {
  785. ret = -EIO;
  786. }
  787. /*
  788. * ugly interaction with invalidatepage here.
  789. * reiserfs_invalidate_page will pin any buffer that has a
  790. * valid journal head from an older transaction. If someone
  791. * else sets our buffer dirty after we write it in the first
  792. * loop, and then someone truncates the page away, nobody
  793. * will ever write the buffer. We're safe if we write the
  794. * page one last time after freeing the journal header.
  795. */
  796. if (buffer_dirty(bh) && unlikely(bh->b_page->mapping == NULL)) {
  797. spin_unlock(lock);
  798. ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
  799. spin_lock(lock);
  800. }
  801. put_bh(bh);
  802. cond_resched_lock(lock);
  803. }
  804. spin_unlock(lock);
  805. return ret;
  806. }
  807. static int flush_older_commits(struct super_block *s,
  808. struct reiserfs_journal_list *jl)
  809. {
  810. struct reiserfs_journal *journal = SB_JOURNAL(s);
  811. struct reiserfs_journal_list *other_jl;
  812. struct reiserfs_journal_list *first_jl;
  813. struct list_head *entry;
  814. unsigned int trans_id = jl->j_trans_id;
  815. unsigned int other_trans_id;
  816. unsigned int first_trans_id;
  817. find_first:
  818. /*
  819. * first we walk backwards to find the oldest uncommitted transation
  820. */
  821. first_jl = jl;
  822. entry = jl->j_list.prev;
  823. while (1) {
  824. other_jl = JOURNAL_LIST_ENTRY(entry);
  825. if (entry == &journal->j_journal_list ||
  826. atomic_read(&other_jl->j_older_commits_done))
  827. break;
  828. first_jl = other_jl;
  829. entry = other_jl->j_list.prev;
  830. }
  831. /* if we didn't find any older uncommitted transactions, return now */
  832. if (first_jl == jl) {
  833. return 0;
  834. }
  835. first_trans_id = first_jl->j_trans_id;
  836. entry = &first_jl->j_list;
  837. while (1) {
  838. other_jl = JOURNAL_LIST_ENTRY(entry);
  839. other_trans_id = other_jl->j_trans_id;
  840. if (other_trans_id < trans_id) {
  841. if (atomic_read(&other_jl->j_commit_left) != 0) {
  842. flush_commit_list(s, other_jl, 0);
  843. /* list we were called with is gone, return */
  844. if (!journal_list_still_alive(s, trans_id))
  845. return 1;
  846. /*
  847. * the one we just flushed is gone, this means
  848. * all older lists are also gone, so first_jl
  849. * is no longer valid either. Go back to the
  850. * beginning.
  851. */
  852. if (!journal_list_still_alive
  853. (s, other_trans_id)) {
  854. goto find_first;
  855. }
  856. }
  857. entry = entry->next;
  858. if (entry == &journal->j_journal_list)
  859. return 0;
  860. } else {
  861. return 0;
  862. }
  863. }
  864. return 0;
  865. }
  866. static int reiserfs_async_progress_wait(struct super_block *s)
  867. {
  868. struct reiserfs_journal *j = SB_JOURNAL(s);
  869. if (atomic_read(&j->j_async_throttle)) {
  870. int depth;
  871. depth = reiserfs_write_unlock_nested(s);
  872. congestion_wait(BLK_RW_ASYNC, HZ / 10);
  873. reiserfs_write_lock_nested(s, depth);
  874. }
  875. return 0;
  876. }
  877. /*
  878. * if this journal list still has commit blocks unflushed, send them to disk.
  879. *
  880. * log areas must be flushed in order (transaction 2 can't commit before
  881. * transaction 1) Before the commit block can by written, every other log
  882. * block must be safely on disk
  883. */
  884. static int flush_commit_list(struct super_block *s,
  885. struct reiserfs_journal_list *jl, int flushall)
  886. {
  887. int i;
  888. b_blocknr_t bn;
  889. struct buffer_head *tbh = NULL;
  890. unsigned int trans_id = jl->j_trans_id;
  891. struct reiserfs_journal *journal = SB_JOURNAL(s);
  892. int retval = 0;
  893. int write_len;
  894. int depth;
  895. reiserfs_check_lock_depth(s, "flush_commit_list");
  896. if (atomic_read(&jl->j_older_commits_done)) {
  897. return 0;
  898. }
  899. /*
  900. * before we can put our commit blocks on disk, we have to make
  901. * sure everyone older than us is on disk too
  902. */
  903. BUG_ON(jl->j_len <= 0);
  904. BUG_ON(trans_id == journal->j_trans_id);
  905. get_journal_list(jl);
  906. if (flushall) {
  907. if (flush_older_commits(s, jl) == 1) {
  908. /*
  909. * list disappeared during flush_older_commits.
  910. * return
  911. */
  912. goto put_jl;
  913. }
  914. }
  915. /* make sure nobody is trying to flush this one at the same time */
  916. reiserfs_mutex_lock_safe(&jl->j_commit_mutex, s);
  917. if (!journal_list_still_alive(s, trans_id)) {
  918. mutex_unlock(&jl->j_commit_mutex);
  919. goto put_jl;
  920. }
  921. BUG_ON(jl->j_trans_id == 0);
  922. /* this commit is done, exit */
  923. if (atomic_read(&jl->j_commit_left) <= 0) {
  924. if (flushall) {
  925. atomic_set(&jl->j_older_commits_done, 1);
  926. }
  927. mutex_unlock(&jl->j_commit_mutex);
  928. goto put_jl;
  929. }
  930. if (!list_empty(&jl->j_bh_list)) {
  931. int ret;
  932. /*
  933. * We might sleep in numerous places inside
  934. * write_ordered_buffers. Relax the write lock.
  935. */
  936. depth = reiserfs_write_unlock_nested(s);
  937. ret = write_ordered_buffers(&journal->j_dirty_buffers_lock,
  938. journal, jl, &jl->j_bh_list);
  939. if (ret < 0 && retval == 0)
  940. retval = ret;
  941. reiserfs_write_lock_nested(s, depth);
  942. }
  943. BUG_ON(!list_empty(&jl->j_bh_list));
  944. /*
  945. * for the description block and all the log blocks, submit any buffers
  946. * that haven't already reached the disk. Try to write at least 256
  947. * log blocks. later on, we will only wait on blocks that correspond
  948. * to this transaction, but while we're unplugging we might as well
  949. * get a chunk of data on there.
  950. */
  951. atomic_inc(&journal->j_async_throttle);
  952. write_len = jl->j_len + 1;
  953. if (write_len < 256)
  954. write_len = 256;
  955. for (i = 0 ; i < write_len ; i++) {
  956. bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start + i) %
  957. SB_ONDISK_JOURNAL_SIZE(s);
  958. tbh = journal_find_get_block(s, bn);
  959. if (tbh) {
  960. if (buffer_dirty(tbh)) {
  961. depth = reiserfs_write_unlock_nested(s);
  962. ll_rw_block(REQ_OP_WRITE, 0, 1, &tbh);
  963. reiserfs_write_lock_nested(s, depth);
  964. }
  965. put_bh(tbh) ;
  966. }
  967. }
  968. atomic_dec(&journal->j_async_throttle);
  969. for (i = 0; i < (jl->j_len + 1); i++) {
  970. bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
  971. (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s);
  972. tbh = journal_find_get_block(s, bn);
  973. depth = reiserfs_write_unlock_nested(s);
  974. __wait_on_buffer(tbh);
  975. reiserfs_write_lock_nested(s, depth);
  976. /*
  977. * since we're using ll_rw_blk above, it might have skipped
  978. * over a locked buffer. Double check here
  979. */
  980. /* redundant, sync_dirty_buffer() checks */
  981. if (buffer_dirty(tbh)) {
  982. depth = reiserfs_write_unlock_nested(s);
  983. sync_dirty_buffer(tbh);
  984. reiserfs_write_lock_nested(s, depth);
  985. }
  986. if (unlikely(!buffer_uptodate(tbh))) {
  987. #ifdef CONFIG_REISERFS_CHECK
  988. reiserfs_warning(s, "journal-601",
  989. "buffer write failed");
  990. #endif
  991. retval = -EIO;
  992. }
  993. /* once for journal_find_get_block */
  994. put_bh(tbh);
  995. /* once due to original getblk in do_journal_end */
  996. put_bh(tbh);
  997. atomic_dec(&jl->j_commit_left);
  998. }
  999. BUG_ON(atomic_read(&jl->j_commit_left) != 1);
  1000. /*
  1001. * If there was a write error in the journal - we can't commit
  1002. * this transaction - it will be invalid and, if successful,
  1003. * will just end up propagating the write error out to
  1004. * the file system.
  1005. */
  1006. if (likely(!retval && !reiserfs_is_journal_aborted (journal))) {
  1007. if (buffer_dirty(jl->j_commit_bh))
  1008. BUG();
  1009. mark_buffer_dirty(jl->j_commit_bh) ;
  1010. depth = reiserfs_write_unlock_nested(s);
  1011. if (reiserfs_barrier_flush(s))
  1012. __sync_dirty_buffer(jl->j_commit_bh,
  1013. REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
  1014. else
  1015. sync_dirty_buffer(jl->j_commit_bh);
  1016. reiserfs_write_lock_nested(s, depth);
  1017. }
  1018. /*
  1019. * If there was a write error in the journal - we can't commit this
  1020. * transaction - it will be invalid and, if successful, will just end
  1021. * up propagating the write error out to the filesystem.
  1022. */
  1023. if (unlikely(!buffer_uptodate(jl->j_commit_bh))) {
  1024. #ifdef CONFIG_REISERFS_CHECK
  1025. reiserfs_warning(s, "journal-615", "buffer write failed");
  1026. #endif
  1027. retval = -EIO;
  1028. }
  1029. bforget(jl->j_commit_bh);
  1030. if (journal->j_last_commit_id != 0 &&
  1031. (jl->j_trans_id - journal->j_last_commit_id) != 1) {
  1032. reiserfs_warning(s, "clm-2200", "last commit %lu, current %lu",
  1033. journal->j_last_commit_id, jl->j_trans_id);
  1034. }
  1035. journal->j_last_commit_id = jl->j_trans_id;
  1036. /*
  1037. * now, every commit block is on the disk. It is safe to allow
  1038. * blocks freed during this transaction to be reallocated
  1039. */
  1040. cleanup_freed_for_journal_list(s, jl);
  1041. retval = retval ? retval : journal->j_errno;
  1042. /* mark the metadata dirty */
  1043. if (!retval)
  1044. dirty_one_transaction(s, jl);
  1045. atomic_dec(&jl->j_commit_left);
  1046. if (flushall) {
  1047. atomic_set(&jl->j_older_commits_done, 1);
  1048. }
  1049. mutex_unlock(&jl->j_commit_mutex);
  1050. put_jl:
  1051. put_journal_list(s, jl);
  1052. if (retval)
  1053. reiserfs_abort(s, retval, "Journal write error in %s",
  1054. __func__);
  1055. return retval;
  1056. }
  1057. /*
  1058. * flush_journal_list frequently needs to find a newer transaction for a
  1059. * given block. This does that, or returns NULL if it can't find anything
  1060. */
  1061. static struct reiserfs_journal_list *find_newer_jl_for_cn(struct
  1062. reiserfs_journal_cnode
  1063. *cn)
  1064. {
  1065. struct super_block *sb = cn->sb;
  1066. b_blocknr_t blocknr = cn->blocknr;
  1067. cn = cn->hprev;
  1068. while (cn) {
  1069. if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
  1070. return cn->jlist;
  1071. }
  1072. cn = cn->hprev;
  1073. }
  1074. return NULL;
  1075. }
  1076. static void remove_journal_hash(struct super_block *,
  1077. struct reiserfs_journal_cnode **,
  1078. struct reiserfs_journal_list *, unsigned long,
  1079. int);
  1080. /*
  1081. * once all the real blocks have been flushed, it is safe to remove them
  1082. * from the journal list for this transaction. Aside from freeing the
  1083. * cnode, this also allows the block to be reallocated for data blocks
  1084. * if it had been deleted.
  1085. */
  1086. static void remove_all_from_journal_list(struct super_block *sb,
  1087. struct reiserfs_journal_list *jl,
  1088. int debug)
  1089. {
  1090. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  1091. struct reiserfs_journal_cnode *cn, *last;
  1092. cn = jl->j_realblock;
  1093. /*
  1094. * which is better, to lock once around the whole loop, or
  1095. * to lock for each call to remove_journal_hash?
  1096. */
  1097. while (cn) {
  1098. if (cn->blocknr != 0) {
  1099. if (debug) {
  1100. reiserfs_warning(sb, "reiserfs-2201",
  1101. "block %u, bh is %d, state %ld",
  1102. cn->blocknr, cn->bh ? 1 : 0,
  1103. cn->state);
  1104. }
  1105. cn->state = 0;
  1106. remove_journal_hash(sb, journal->j_list_hash_table,
  1107. jl, cn->blocknr, 1);
  1108. }
  1109. last = cn;
  1110. cn = cn->next;
  1111. free_cnode(sb, last);
  1112. }
  1113. jl->j_realblock = NULL;
  1114. }
  1115. /*
  1116. * if this timestamp is greater than the timestamp we wrote last to the
  1117. * header block, write it to the header block. once this is done, I can
  1118. * safely say the log area for this transaction won't ever be replayed,
  1119. * and I can start releasing blocks in this transaction for reuse as data
  1120. * blocks. called by flush_journal_list, before it calls
  1121. * remove_all_from_journal_list
  1122. */
  1123. static int _update_journal_header_block(struct super_block *sb,
  1124. unsigned long offset,
  1125. unsigned int trans_id)
  1126. {
  1127. struct reiserfs_journal_header *jh;
  1128. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  1129. int depth;
  1130. if (reiserfs_is_journal_aborted(journal))
  1131. return -EIO;
  1132. if (trans_id >= journal->j_last_flush_trans_id) {
  1133. if (buffer_locked((journal->j_header_bh))) {
  1134. depth = reiserfs_write_unlock_nested(sb);
  1135. __wait_on_buffer(journal->j_header_bh);
  1136. reiserfs_write_lock_nested(sb, depth);
  1137. if (unlikely(!buffer_uptodate(journal->j_header_bh))) {
  1138. #ifdef CONFIG_REISERFS_CHECK
  1139. reiserfs_warning(sb, "journal-699",
  1140. "buffer write failed");
  1141. #endif
  1142. return -EIO;
  1143. }
  1144. }
  1145. journal->j_last_flush_trans_id = trans_id;
  1146. journal->j_first_unflushed_offset = offset;
  1147. jh = (struct reiserfs_journal_header *)(journal->j_header_bh->
  1148. b_data);
  1149. jh->j_last_flush_trans_id = cpu_to_le32(trans_id);
  1150. jh->j_first_unflushed_offset = cpu_to_le32(offset);
  1151. jh->j_mount_id = cpu_to_le32(journal->j_mount_id);
  1152. set_buffer_dirty(journal->j_header_bh);
  1153. depth = reiserfs_write_unlock_nested(sb);
  1154. if (reiserfs_barrier_flush(sb))
  1155. __sync_dirty_buffer(journal->j_header_bh,
  1156. REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
  1157. else
  1158. sync_dirty_buffer(journal->j_header_bh);
  1159. reiserfs_write_lock_nested(sb, depth);
  1160. if (!buffer_uptodate(journal->j_header_bh)) {
  1161. reiserfs_warning(sb, "journal-837",
  1162. "IO error during journal replay");
  1163. return -EIO;
  1164. }
  1165. }
  1166. return 0;
  1167. }
  1168. static int update_journal_header_block(struct super_block *sb,
  1169. unsigned long offset,
  1170. unsigned int trans_id)
  1171. {
  1172. return _update_journal_header_block(sb, offset, trans_id);
  1173. }
  1174. /*
  1175. ** flush any and all journal lists older than you are
  1176. ** can only be called from flush_journal_list
  1177. */
  1178. static int flush_older_journal_lists(struct super_block *sb,
  1179. struct reiserfs_journal_list *jl)
  1180. {
  1181. struct list_head *entry;
  1182. struct reiserfs_journal_list *other_jl;
  1183. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  1184. unsigned int trans_id = jl->j_trans_id;
  1185. /*
  1186. * we know we are the only ones flushing things, no extra race
  1187. * protection is required.
  1188. */
  1189. restart:
  1190. entry = journal->j_journal_list.next;
  1191. /* Did we wrap? */
  1192. if (entry == &journal->j_journal_list)
  1193. return 0;
  1194. other_jl = JOURNAL_LIST_ENTRY(entry);
  1195. if (other_jl->j_trans_id < trans_id) {
  1196. BUG_ON(other_jl->j_refcount <= 0);
  1197. /* do not flush all */
  1198. flush_journal_list(sb, other_jl, 0);
  1199. /* other_jl is now deleted from the list */
  1200. goto restart;
  1201. }
  1202. return 0;
  1203. }
  1204. static void del_from_work_list(struct super_block *s,
  1205. struct reiserfs_journal_list *jl)
  1206. {
  1207. struct reiserfs_journal *journal = SB_JOURNAL(s);
  1208. if (!list_empty(&jl->j_working_list)) {
  1209. list_del_init(&jl->j_working_list);
  1210. journal->j_num_work_lists--;
  1211. }
  1212. }
  1213. /*
  1214. * flush a journal list, both commit and real blocks
  1215. *
  1216. * always set flushall to 1, unless you are calling from inside
  1217. * flush_journal_list
  1218. *
  1219. * IMPORTANT. This can only be called while there are no journal writers,
  1220. * and the journal is locked. That means it can only be called from
  1221. * do_journal_end, or by journal_release
  1222. */
  1223. static int flush_journal_list(struct super_block *s,
  1224. struct reiserfs_journal_list *jl, int flushall)
  1225. {
  1226. struct reiserfs_journal_list *pjl;
  1227. struct reiserfs_journal_cnode *cn, *last;
  1228. int count;
  1229. int was_jwait = 0;
  1230. int was_dirty = 0;
  1231. struct buffer_head *saved_bh;
  1232. unsigned long j_len_saved = jl->j_len;
  1233. struct reiserfs_journal *journal = SB_JOURNAL(s);
  1234. int err = 0;
  1235. int depth;
  1236. BUG_ON(j_len_saved <= 0);
  1237. if (atomic_read(&journal->j_wcount) != 0) {
  1238. reiserfs_warning(s, "clm-2048", "called with wcount %d",
  1239. atomic_read(&journal->j_wcount));
  1240. }
  1241. /* if flushall == 0, the lock is already held */
  1242. if (flushall) {
  1243. reiserfs_mutex_lock_safe(&journal->j_flush_mutex, s);
  1244. } else if (mutex_trylock(&journal->j_flush_mutex)) {
  1245. BUG();
  1246. }
  1247. count = 0;
  1248. if (j_len_saved > journal->j_trans_max) {
  1249. reiserfs_panic(s, "journal-715", "length is %lu, trans id %lu",
  1250. j_len_saved, jl->j_trans_id);
  1251. return 0;
  1252. }
  1253. /* if all the work is already done, get out of here */
  1254. if (atomic_read(&jl->j_nonzerolen) <= 0 &&
  1255. atomic_read(&jl->j_commit_left) <= 0) {
  1256. goto flush_older_and_return;
  1257. }
  1258. /*
  1259. * start by putting the commit list on disk. This will also flush
  1260. * the commit lists of any olders transactions
  1261. */
  1262. flush_commit_list(s, jl, 1);
  1263. if (!(jl->j_state & LIST_DIRTY)
  1264. && !reiserfs_is_journal_aborted(journal))
  1265. BUG();
  1266. /* are we done now? */
  1267. if (atomic_read(&jl->j_nonzerolen) <= 0 &&
  1268. atomic_read(&jl->j_commit_left) <= 0) {
  1269. goto flush_older_and_return;
  1270. }
  1271. /*
  1272. * loop through each cnode, see if we need to write it,
  1273. * or wait on a more recent transaction, or just ignore it
  1274. */
  1275. if (atomic_read(&journal->j_wcount) != 0) {
  1276. reiserfs_panic(s, "journal-844", "journal list is flushing, "
  1277. "wcount is not 0");
  1278. }
  1279. cn = jl->j_realblock;
  1280. while (cn) {
  1281. was_jwait = 0;
  1282. was_dirty = 0;
  1283. saved_bh = NULL;
  1284. /* blocknr of 0 is no longer in the hash, ignore it */
  1285. if (cn->blocknr == 0) {
  1286. goto free_cnode;
  1287. }
  1288. /*
  1289. * This transaction failed commit.
  1290. * Don't write out to the disk
  1291. */
  1292. if (!(jl->j_state & LIST_DIRTY))
  1293. goto free_cnode;
  1294. pjl = find_newer_jl_for_cn(cn);
  1295. /*
  1296. * the order is important here. We check pjl to make sure we
  1297. * don't clear BH_JDirty_wait if we aren't the one writing this
  1298. * block to disk
  1299. */
  1300. if (!pjl && cn->bh) {
  1301. saved_bh = cn->bh;
  1302. /*
  1303. * we do this to make sure nobody releases the
  1304. * buffer while we are working with it
  1305. */
  1306. get_bh(saved_bh);
  1307. if (buffer_journal_dirty(saved_bh)) {
  1308. BUG_ON(!can_dirty(cn));
  1309. was_jwait = 1;
  1310. was_dirty = 1;
  1311. } else if (can_dirty(cn)) {
  1312. /*
  1313. * everything with !pjl && jwait
  1314. * should be writable
  1315. */
  1316. BUG();
  1317. }
  1318. }
  1319. /*
  1320. * if someone has this block in a newer transaction, just make
  1321. * sure they are committed, and don't try writing it to disk
  1322. */
  1323. if (pjl) {
  1324. if (atomic_read(&pjl->j_commit_left))
  1325. flush_commit_list(s, pjl, 1);
  1326. goto free_cnode;
  1327. }
  1328. /*
  1329. * bh == NULL when the block got to disk on its own, OR,
  1330. * the block got freed in a future transaction
  1331. */
  1332. if (saved_bh == NULL) {
  1333. goto free_cnode;
  1334. }
  1335. /*
  1336. * this should never happen. kupdate_one_transaction has
  1337. * this list locked while it works, so we should never see a
  1338. * buffer here that is not marked JDirty_wait
  1339. */
  1340. if ((!was_jwait) && !buffer_locked(saved_bh)) {
  1341. reiserfs_warning(s, "journal-813",
  1342. "BAD! buffer %llu %cdirty %cjwait, "
  1343. "not in a newer transaction",
  1344. (unsigned long long)saved_bh->
  1345. b_blocknr, was_dirty ? ' ' : '!',
  1346. was_jwait ? ' ' : '!');
  1347. }
  1348. if (was_dirty) {
  1349. /*
  1350. * we inc again because saved_bh gets decremented
  1351. * at free_cnode
  1352. */
  1353. get_bh(saved_bh);
  1354. set_bit(BLOCK_NEEDS_FLUSH, &cn->state);
  1355. lock_buffer(saved_bh);
  1356. BUG_ON(cn->blocknr != saved_bh->b_blocknr);
  1357. if (buffer_dirty(saved_bh))
  1358. submit_logged_buffer(saved_bh);
  1359. else
  1360. unlock_buffer(saved_bh);
  1361. count++;
  1362. } else {
  1363. reiserfs_warning(s, "clm-2082",
  1364. "Unable to flush buffer %llu in %s",
  1365. (unsigned long long)saved_bh->
  1366. b_blocknr, __func__);
  1367. }
  1368. free_cnode:
  1369. last = cn;
  1370. cn = cn->next;
  1371. if (saved_bh) {
  1372. /*
  1373. * we incremented this to keep others from
  1374. * taking the buffer head away
  1375. */
  1376. put_bh(saved_bh);
  1377. if (atomic_read(&saved_bh->b_count) < 0) {
  1378. reiserfs_warning(s, "journal-945",
  1379. "saved_bh->b_count < 0");
  1380. }
  1381. }
  1382. }
  1383. if (count > 0) {
  1384. cn = jl->j_realblock;
  1385. while (cn) {
  1386. if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
  1387. if (!cn->bh) {
  1388. reiserfs_panic(s, "journal-1011",
  1389. "cn->bh is NULL");
  1390. }
  1391. depth = reiserfs_write_unlock_nested(s);
  1392. __wait_on_buffer(cn->bh);
  1393. reiserfs_write_lock_nested(s, depth);
  1394. if (!cn->bh) {
  1395. reiserfs_panic(s, "journal-1012",
  1396. "cn->bh is NULL");
  1397. }
  1398. if (unlikely(!buffer_uptodate(cn->bh))) {
  1399. #ifdef CONFIG_REISERFS_CHECK
  1400. reiserfs_warning(s, "journal-949",
  1401. "buffer write failed");
  1402. #endif
  1403. err = -EIO;
  1404. }
  1405. /*
  1406. * note, we must clear the JDirty_wait bit
  1407. * after the up to date check, otherwise we
  1408. * race against our flushpage routine
  1409. */
  1410. BUG_ON(!test_clear_buffer_journal_dirty
  1411. (cn->bh));
  1412. /* drop one ref for us */
  1413. put_bh(cn->bh);
  1414. /* drop one ref for journal_mark_dirty */
  1415. release_buffer_page(cn->bh);
  1416. }
  1417. cn = cn->next;
  1418. }
  1419. }
  1420. if (err)
  1421. reiserfs_abort(s, -EIO,
  1422. "Write error while pushing transaction to disk in %s",
  1423. __func__);
  1424. flush_older_and_return:
  1425. /*
  1426. * before we can update the journal header block, we _must_ flush all
  1427. * real blocks from all older transactions to disk. This is because
  1428. * once the header block is updated, this transaction will not be
  1429. * replayed after a crash
  1430. */
  1431. if (flushall) {
  1432. flush_older_journal_lists(s, jl);
  1433. }
  1434. err = journal->j_errno;
  1435. /*
  1436. * before we can remove everything from the hash tables for this
  1437. * transaction, we must make sure it can never be replayed
  1438. *
  1439. * since we are only called from do_journal_end, we know for sure there
  1440. * are no allocations going on while we are flushing journal lists. So,
  1441. * we only need to update the journal header block for the last list
  1442. * being flushed
  1443. */
  1444. if (!err && flushall) {
  1445. err =
  1446. update_journal_header_block(s,
  1447. (jl->j_start + jl->j_len +
  1448. 2) % SB_ONDISK_JOURNAL_SIZE(s),
  1449. jl->j_trans_id);
  1450. if (err)
  1451. reiserfs_abort(s, -EIO,
  1452. "Write error while updating journal header in %s",
  1453. __func__);
  1454. }
  1455. remove_all_from_journal_list(s, jl, 0);
  1456. list_del_init(&jl->j_list);
  1457. journal->j_num_lists--;
  1458. del_from_work_list(s, jl);
  1459. if (journal->j_last_flush_id != 0 &&
  1460. (jl->j_trans_id - journal->j_last_flush_id) != 1) {
  1461. reiserfs_warning(s, "clm-2201", "last flush %lu, current %lu",
  1462. journal->j_last_flush_id, jl->j_trans_id);
  1463. }
  1464. journal->j_last_flush_id = jl->j_trans_id;
  1465. /*
  1466. * not strictly required since we are freeing the list, but it should
  1467. * help find code using dead lists later on
  1468. */
  1469. jl->j_len = 0;
  1470. atomic_set(&jl->j_nonzerolen, 0);
  1471. jl->j_start = 0;
  1472. jl->j_realblock = NULL;
  1473. jl->j_commit_bh = NULL;
  1474. jl->j_trans_id = 0;
  1475. jl->j_state = 0;
  1476. put_journal_list(s, jl);
  1477. if (flushall)
  1478. mutex_unlock(&journal->j_flush_mutex);
  1479. return err;
  1480. }
  1481. static int write_one_transaction(struct super_block *s,
  1482. struct reiserfs_journal_list *jl,
  1483. struct buffer_chunk *chunk)
  1484. {
  1485. struct reiserfs_journal_cnode *cn;
  1486. int ret = 0;
  1487. jl->j_state |= LIST_TOUCHED;
  1488. del_from_work_list(s, jl);
  1489. if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
  1490. return 0;
  1491. }
  1492. cn = jl->j_realblock;
  1493. while (cn) {
  1494. /*
  1495. * if the blocknr == 0, this has been cleared from the hash,
  1496. * skip it
  1497. */
  1498. if (cn->blocknr == 0) {
  1499. goto next;
  1500. }
  1501. if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
  1502. struct buffer_head *tmp_bh;
  1503. /*
  1504. * we can race against journal_mark_freed when we try
  1505. * to lock_buffer(cn->bh), so we have to inc the buffer
  1506. * count, and recheck things after locking
  1507. */
  1508. tmp_bh = cn->bh;
  1509. get_bh(tmp_bh);
  1510. lock_buffer(tmp_bh);
  1511. if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
  1512. if (!buffer_journal_dirty(tmp_bh) ||
  1513. buffer_journal_prepared(tmp_bh))
  1514. BUG();
  1515. add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
  1516. ret++;
  1517. } else {
  1518. /* note, cn->bh might be null now */
  1519. unlock_buffer(tmp_bh);
  1520. }
  1521. put_bh(tmp_bh);
  1522. }
  1523. next:
  1524. cn = cn->next;
  1525. cond_resched();
  1526. }
  1527. return ret;
  1528. }
  1529. /* used by flush_commit_list */
  1530. static int dirty_one_transaction(struct super_block *s,
  1531. struct reiserfs_journal_list *jl)
  1532. {
  1533. struct reiserfs_journal_cnode *cn;
  1534. struct reiserfs_journal_list *pjl;
  1535. int ret = 0;
  1536. jl->j_state |= LIST_DIRTY;
  1537. cn = jl->j_realblock;
  1538. while (cn) {
  1539. /*
  1540. * look for a more recent transaction that logged this
  1541. * buffer. Only the most recent transaction with a buffer in
  1542. * it is allowed to send that buffer to disk
  1543. */
  1544. pjl = find_newer_jl_for_cn(cn);
  1545. if (!pjl && cn->blocknr && cn->bh
  1546. && buffer_journal_dirty(cn->bh)) {
  1547. BUG_ON(!can_dirty(cn));
  1548. /*
  1549. * if the buffer is prepared, it will either be logged
  1550. * or restored. If restored, we need to make sure
  1551. * it actually gets marked dirty
  1552. */
  1553. clear_buffer_journal_new(cn->bh);
  1554. if (buffer_journal_prepared(cn->bh)) {
  1555. set_buffer_journal_restore_dirty(cn->bh);
  1556. } else {
  1557. set_buffer_journal_test(cn->bh);
  1558. mark_buffer_dirty(cn->bh);
  1559. }
  1560. }
  1561. cn = cn->next;
  1562. }
  1563. return ret;
  1564. }
  1565. static int kupdate_transactions(struct super_block *s,
  1566. struct reiserfs_journal_list *jl,
  1567. struct reiserfs_journal_list **next_jl,
  1568. unsigned int *next_trans_id,
  1569. int num_blocks, int num_trans)
  1570. {
  1571. int ret = 0;
  1572. int written = 0;
  1573. int transactions_flushed = 0;
  1574. unsigned int orig_trans_id = jl->j_trans_id;
  1575. struct buffer_chunk chunk;
  1576. struct list_head *entry;
  1577. struct reiserfs_journal *journal = SB_JOURNAL(s);
  1578. chunk.nr = 0;
  1579. reiserfs_mutex_lock_safe(&journal->j_flush_mutex, s);
  1580. if (!journal_list_still_alive(s, orig_trans_id)) {
  1581. goto done;
  1582. }
  1583. /*
  1584. * we've got j_flush_mutex held, nobody is going to delete any
  1585. * of these lists out from underneath us
  1586. */
  1587. while ((num_trans && transactions_flushed < num_trans) ||
  1588. (!num_trans && written < num_blocks)) {
  1589. if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
  1590. atomic_read(&jl->j_commit_left)
  1591. || !(jl->j_state & LIST_DIRTY)) {
  1592. del_from_work_list(s, jl);
  1593. break;
  1594. }
  1595. ret = write_one_transaction(s, jl, &chunk);
  1596. if (ret < 0)
  1597. goto done;
  1598. transactions_flushed++;
  1599. written += ret;
  1600. entry = jl->j_list.next;
  1601. /* did we wrap? */
  1602. if (entry == &journal->j_journal_list) {
  1603. break;
  1604. }
  1605. jl = JOURNAL_LIST_ENTRY(entry);
  1606. /* don't bother with older transactions */
  1607. if (jl->j_trans_id <= orig_trans_id)
  1608. break;
  1609. }
  1610. if (chunk.nr) {
  1611. write_chunk(&chunk);
  1612. }
  1613. done:
  1614. mutex_unlock(&journal->j_flush_mutex);
  1615. return ret;
  1616. }
  1617. /*
  1618. * for o_sync and fsync heavy applications, they tend to use
  1619. * all the journa list slots with tiny transactions. These
  1620. * trigger lots and lots of calls to update the header block, which
  1621. * adds seeks and slows things down.
  1622. *
  1623. * This function tries to clear out a large chunk of the journal lists
  1624. * at once, which makes everything faster since only the newest journal
  1625. * list updates the header block
  1626. */
  1627. static int flush_used_journal_lists(struct super_block *s,
  1628. struct reiserfs_journal_list *jl)
  1629. {
  1630. unsigned long len = 0;
  1631. unsigned long cur_len;
  1632. int ret;
  1633. int i;
  1634. int limit = 256;
  1635. struct reiserfs_journal_list *tjl;
  1636. struct reiserfs_journal_list *flush_jl;
  1637. unsigned int trans_id;
  1638. struct reiserfs_journal *journal = SB_JOURNAL(s);
  1639. flush_jl = tjl = jl;
  1640. /* in data logging mode, try harder to flush a lot of blocks */
  1641. if (reiserfs_data_log(s))
  1642. limit = 1024;
  1643. /* flush for 256 transactions or limit blocks, whichever comes first */
  1644. for (i = 0; i < 256 && len < limit; i++) {
  1645. if (atomic_read(&tjl->j_commit_left) ||
  1646. tjl->j_trans_id < jl->j_trans_id) {
  1647. break;
  1648. }
  1649. cur_len = atomic_read(&tjl->j_nonzerolen);
  1650. if (cur_len > 0) {
  1651. tjl->j_state &= ~LIST_TOUCHED;
  1652. }
  1653. len += cur_len;
  1654. flush_jl = tjl;
  1655. if (tjl->j_list.next == &journal->j_journal_list)
  1656. break;
  1657. tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
  1658. }
  1659. get_journal_list(jl);
  1660. get_journal_list(flush_jl);
  1661. /*
  1662. * try to find a group of blocks we can flush across all the
  1663. * transactions, but only bother if we've actually spanned
  1664. * across multiple lists
  1665. */
  1666. if (flush_jl != jl) {
  1667. ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
  1668. }
  1669. flush_journal_list(s, flush_jl, 1);
  1670. put_journal_list(s, flush_jl);
  1671. put_journal_list(s, jl);
  1672. return 0;
  1673. }
  1674. /*
  1675. * removes any nodes in table with name block and dev as bh.
  1676. * only touchs the hnext and hprev pointers.
  1677. */
  1678. void remove_journal_hash(struct super_block *sb,
  1679. struct reiserfs_journal_cnode **table,
  1680. struct reiserfs_journal_list *jl,
  1681. unsigned long block, int remove_freed)
  1682. {
  1683. struct reiserfs_journal_cnode *cur;
  1684. struct reiserfs_journal_cnode **head;
  1685. head = &(journal_hash(table, sb, block));
  1686. if (!head) {
  1687. return;
  1688. }
  1689. cur = *head;
  1690. while (cur) {
  1691. if (cur->blocknr == block && cur->sb == sb
  1692. && (jl == NULL || jl == cur->jlist)
  1693. && (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
  1694. if (cur->hnext) {
  1695. cur->hnext->hprev = cur->hprev;
  1696. }
  1697. if (cur->hprev) {
  1698. cur->hprev->hnext = cur->hnext;
  1699. } else {
  1700. *head = cur->hnext;
  1701. }
  1702. cur->blocknr = 0;
  1703. cur->sb = NULL;
  1704. cur->state = 0;
  1705. /*
  1706. * anybody who clears the cur->bh will also
  1707. * dec the nonzerolen
  1708. */
  1709. if (cur->bh && cur->jlist)
  1710. atomic_dec(&cur->jlist->j_nonzerolen);
  1711. cur->bh = NULL;
  1712. cur->jlist = NULL;
  1713. }
  1714. cur = cur->hnext;
  1715. }
  1716. }
  1717. static void free_journal_ram(struct super_block *sb)
  1718. {
  1719. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  1720. kfree(journal->j_current_jl);
  1721. journal->j_num_lists--;
  1722. vfree(journal->j_cnode_free_orig);
  1723. free_list_bitmaps(sb, journal->j_list_bitmap);
  1724. free_bitmap_nodes(sb); /* must be after free_list_bitmaps */
  1725. if (journal->j_header_bh) {
  1726. brelse(journal->j_header_bh);
  1727. }
  1728. /*
  1729. * j_header_bh is on the journal dev, make sure
  1730. * not to release the journal dev until we brelse j_header_bh
  1731. */
  1732. release_journal_dev(sb, journal);
  1733. vfree(journal);
  1734. }
  1735. /*
  1736. * call on unmount. Only set error to 1 if you haven't made your way out
  1737. * of read_super() yet. Any other caller must keep error at 0.
  1738. */
  1739. static int do_journal_release(struct reiserfs_transaction_handle *th,
  1740. struct super_block *sb, int error)
  1741. {
  1742. struct reiserfs_transaction_handle myth;
  1743. int flushed = 0;
  1744. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  1745. /*
  1746. * we only want to flush out transactions if we were
  1747. * called with error == 0
  1748. */
  1749. if (!error && !sb_rdonly(sb)) {
  1750. /* end the current trans */
  1751. BUG_ON(!th->t_trans_id);
  1752. do_journal_end(th, FLUSH_ALL);
  1753. /*
  1754. * make sure something gets logged to force
  1755. * our way into the flush code
  1756. */
  1757. if (!journal_join(&myth, sb)) {
  1758. reiserfs_prepare_for_journal(sb,
  1759. SB_BUFFER_WITH_SB(sb),
  1760. 1);
  1761. journal_mark_dirty(&myth, SB_BUFFER_WITH_SB(sb));
  1762. do_journal_end(&myth, FLUSH_ALL);
  1763. flushed = 1;
  1764. }
  1765. }
  1766. /* this also catches errors during the do_journal_end above */
  1767. if (!error && reiserfs_is_journal_aborted(journal)) {
  1768. memset(&myth, 0, sizeof(myth));
  1769. if (!journal_join_abort(&myth, sb)) {
  1770. reiserfs_prepare_for_journal(sb,
  1771. SB_BUFFER_WITH_SB(sb),
  1772. 1);
  1773. journal_mark_dirty(&myth, SB_BUFFER_WITH_SB(sb));
  1774. do_journal_end(&myth, FLUSH_ALL);
  1775. }
  1776. }
  1777. /*
  1778. * We must release the write lock here because
  1779. * the workqueue job (flush_async_commit) needs this lock
  1780. */
  1781. reiserfs_write_unlock(sb);
  1782. /*
  1783. * Cancel flushing of old commits. Note that neither of these works
  1784. * will be requeued because superblock is being shutdown and doesn't
  1785. * have SB_ACTIVE set.
  1786. */
  1787. reiserfs_cancel_old_flush(sb);
  1788. /* wait for all commits to finish */
  1789. cancel_delayed_work_sync(&SB_JOURNAL(sb)->j_work);
  1790. free_journal_ram(sb);
  1791. reiserfs_write_lock(sb);
  1792. return 0;
  1793. }
  1794. /* * call on unmount. flush all journal trans, release all alloc'd ram */
  1795. int journal_release(struct reiserfs_transaction_handle *th,
  1796. struct super_block *sb)
  1797. {
  1798. return do_journal_release(th, sb, 0);
  1799. }
  1800. /* only call from an error condition inside reiserfs_read_super! */
  1801. int journal_release_error(struct reiserfs_transaction_handle *th,
  1802. struct super_block *sb)
  1803. {
  1804. return do_journal_release(th, sb, 1);
  1805. }
  1806. /*
  1807. * compares description block with commit block.
  1808. * returns 1 if they differ, 0 if they are the same
  1809. */
  1810. static int journal_compare_desc_commit(struct super_block *sb,
  1811. struct reiserfs_journal_desc *desc,
  1812. struct reiserfs_journal_commit *commit)
  1813. {
  1814. if (get_commit_trans_id(commit) != get_desc_trans_id(desc) ||
  1815. get_commit_trans_len(commit) != get_desc_trans_len(desc) ||
  1816. get_commit_trans_len(commit) > SB_JOURNAL(sb)->j_trans_max ||
  1817. get_commit_trans_len(commit) <= 0) {
  1818. return 1;
  1819. }
  1820. return 0;
  1821. }
  1822. /*
  1823. * returns 0 if it did not find a description block
  1824. * returns -1 if it found a corrupt commit block
  1825. * returns 1 if both desc and commit were valid
  1826. * NOTE: only called during fs mount
  1827. */
  1828. static int journal_transaction_is_valid(struct super_block *sb,
  1829. struct buffer_head *d_bh,
  1830. unsigned int *oldest_invalid_trans_id,
  1831. unsigned long *newest_mount_id)
  1832. {
  1833. struct reiserfs_journal_desc *desc;
  1834. struct reiserfs_journal_commit *commit;
  1835. struct buffer_head *c_bh;
  1836. unsigned long offset;
  1837. if (!d_bh)
  1838. return 0;
  1839. desc = (struct reiserfs_journal_desc *)d_bh->b_data;
  1840. if (get_desc_trans_len(desc) > 0
  1841. && !memcmp(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8)) {
  1842. if (oldest_invalid_trans_id && *oldest_invalid_trans_id
  1843. && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
  1844. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1845. "journal-986: transaction "
  1846. "is valid returning because trans_id %d is greater than "
  1847. "oldest_invalid %lu",
  1848. get_desc_trans_id(desc),
  1849. *oldest_invalid_trans_id);
  1850. return 0;
  1851. }
  1852. if (newest_mount_id
  1853. && *newest_mount_id > get_desc_mount_id(desc)) {
  1854. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1855. "journal-1087: transaction "
  1856. "is valid returning because mount_id %d is less than "
  1857. "newest_mount_id %lu",
  1858. get_desc_mount_id(desc),
  1859. *newest_mount_id);
  1860. return -1;
  1861. }
  1862. if (get_desc_trans_len(desc) > SB_JOURNAL(sb)->j_trans_max) {
  1863. reiserfs_warning(sb, "journal-2018",
  1864. "Bad transaction length %d "
  1865. "encountered, ignoring transaction",
  1866. get_desc_trans_len(desc));
  1867. return -1;
  1868. }
  1869. offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb);
  1870. /*
  1871. * ok, we have a journal description block,
  1872. * let's see if the transaction was valid
  1873. */
  1874. c_bh =
  1875. journal_bread(sb,
  1876. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  1877. ((offset + get_desc_trans_len(desc) +
  1878. 1) % SB_ONDISK_JOURNAL_SIZE(sb)));
  1879. if (!c_bh)
  1880. return 0;
  1881. commit = (struct reiserfs_journal_commit *)c_bh->b_data;
  1882. if (journal_compare_desc_commit(sb, desc, commit)) {
  1883. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1884. "journal_transaction_is_valid, commit offset %ld had bad "
  1885. "time %d or length %d",
  1886. c_bh->b_blocknr -
  1887. SB_ONDISK_JOURNAL_1st_BLOCK(sb),
  1888. get_commit_trans_id(commit),
  1889. get_commit_trans_len(commit));
  1890. brelse(c_bh);
  1891. if (oldest_invalid_trans_id) {
  1892. *oldest_invalid_trans_id =
  1893. get_desc_trans_id(desc);
  1894. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1895. "journal-1004: "
  1896. "transaction_is_valid setting oldest invalid trans_id "
  1897. "to %d",
  1898. get_desc_trans_id(desc));
  1899. }
  1900. return -1;
  1901. }
  1902. brelse(c_bh);
  1903. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1904. "journal-1006: found valid "
  1905. "transaction start offset %llu, len %d id %d",
  1906. d_bh->b_blocknr -
  1907. SB_ONDISK_JOURNAL_1st_BLOCK(sb),
  1908. get_desc_trans_len(desc),
  1909. get_desc_trans_id(desc));
  1910. return 1;
  1911. } else {
  1912. return 0;
  1913. }
  1914. }
  1915. static void brelse_array(struct buffer_head **heads, int num)
  1916. {
  1917. int i;
  1918. for (i = 0; i < num; i++) {
  1919. brelse(heads[i]);
  1920. }
  1921. }
  1922. /*
  1923. * given the start, and values for the oldest acceptable transactions,
  1924. * this either reads in a replays a transaction, or returns because the
  1925. * transaction is invalid, or too old.
  1926. * NOTE: only called during fs mount
  1927. */
  1928. static int journal_read_transaction(struct super_block *sb,
  1929. unsigned long cur_dblock,
  1930. unsigned long oldest_start,
  1931. unsigned int oldest_trans_id,
  1932. unsigned long newest_mount_id)
  1933. {
  1934. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  1935. struct reiserfs_journal_desc *desc;
  1936. struct reiserfs_journal_commit *commit;
  1937. unsigned int trans_id = 0;
  1938. struct buffer_head *c_bh;
  1939. struct buffer_head *d_bh;
  1940. struct buffer_head **log_blocks = NULL;
  1941. struct buffer_head **real_blocks = NULL;
  1942. unsigned int trans_offset;
  1943. int i;
  1944. int trans_half;
  1945. d_bh = journal_bread(sb, cur_dblock);
  1946. if (!d_bh)
  1947. return 1;
  1948. desc = (struct reiserfs_journal_desc *)d_bh->b_data;
  1949. trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb);
  1950. reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1037: "
  1951. "journal_read_transaction, offset %llu, len %d mount_id %d",
  1952. d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb),
  1953. get_desc_trans_len(desc), get_desc_mount_id(desc));
  1954. if (get_desc_trans_id(desc) < oldest_trans_id) {
  1955. reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1039: "
  1956. "journal_read_trans skipping because %lu is too old",
  1957. cur_dblock -
  1958. SB_ONDISK_JOURNAL_1st_BLOCK(sb));
  1959. brelse(d_bh);
  1960. return 1;
  1961. }
  1962. if (get_desc_mount_id(desc) != newest_mount_id) {
  1963. reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1146: "
  1964. "journal_read_trans skipping because %d is != "
  1965. "newest_mount_id %lu", get_desc_mount_id(desc),
  1966. newest_mount_id);
  1967. brelse(d_bh);
  1968. return 1;
  1969. }
  1970. c_bh = journal_bread(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  1971. ((trans_offset + get_desc_trans_len(desc) + 1) %
  1972. SB_ONDISK_JOURNAL_SIZE(sb)));
  1973. if (!c_bh) {
  1974. brelse(d_bh);
  1975. return 1;
  1976. }
  1977. commit = (struct reiserfs_journal_commit *)c_bh->b_data;
  1978. if (journal_compare_desc_commit(sb, desc, commit)) {
  1979. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1980. "journal_read_transaction, "
  1981. "commit offset %llu had bad time %d or length %d",
  1982. c_bh->b_blocknr -
  1983. SB_ONDISK_JOURNAL_1st_BLOCK(sb),
  1984. get_commit_trans_id(commit),
  1985. get_commit_trans_len(commit));
  1986. brelse(c_bh);
  1987. brelse(d_bh);
  1988. return 1;
  1989. }
  1990. if (bdev_read_only(sb->s_bdev)) {
  1991. reiserfs_warning(sb, "clm-2076",
  1992. "device is readonly, unable to replay log");
  1993. brelse(c_bh);
  1994. brelse(d_bh);
  1995. return -EROFS;
  1996. }
  1997. trans_id = get_desc_trans_id(desc);
  1998. /*
  1999. * now we know we've got a good transaction, and it was
  2000. * inside the valid time ranges
  2001. */
  2002. log_blocks = kmalloc_array(get_desc_trans_len(desc),
  2003. sizeof(struct buffer_head *),
  2004. GFP_NOFS);
  2005. real_blocks = kmalloc_array(get_desc_trans_len(desc),
  2006. sizeof(struct buffer_head *),
  2007. GFP_NOFS);
  2008. if (!log_blocks || !real_blocks) {
  2009. brelse(c_bh);
  2010. brelse(d_bh);
  2011. kfree(log_blocks);
  2012. kfree(real_blocks);
  2013. reiserfs_warning(sb, "journal-1169",
  2014. "kmalloc failed, unable to mount FS");
  2015. return -1;
  2016. }
  2017. /* get all the buffer heads */
  2018. trans_half = journal_trans_half(sb->s_blocksize);
  2019. for (i = 0; i < get_desc_trans_len(desc); i++) {
  2020. log_blocks[i] =
  2021. journal_getblk(sb,
  2022. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2023. (trans_offset + 1 +
  2024. i) % SB_ONDISK_JOURNAL_SIZE(sb));
  2025. if (i < trans_half) {
  2026. real_blocks[i] =
  2027. sb_getblk(sb,
  2028. le32_to_cpu(desc->j_realblock[i]));
  2029. } else {
  2030. real_blocks[i] =
  2031. sb_getblk(sb,
  2032. le32_to_cpu(commit->
  2033. j_realblock[i - trans_half]));
  2034. }
  2035. if (real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(sb)) {
  2036. reiserfs_warning(sb, "journal-1207",
  2037. "REPLAY FAILURE fsck required! "
  2038. "Block to replay is outside of "
  2039. "filesystem");
  2040. goto abort_replay;
  2041. }
  2042. /* make sure we don't try to replay onto log or reserved area */
  2043. if (is_block_in_log_or_reserved_area
  2044. (sb, real_blocks[i]->b_blocknr)) {
  2045. reiserfs_warning(sb, "journal-1204",
  2046. "REPLAY FAILURE fsck required! "
  2047. "Trying to replay onto a log block");
  2048. abort_replay:
  2049. brelse_array(log_blocks, i);
  2050. brelse_array(real_blocks, i);
  2051. brelse(c_bh);
  2052. brelse(d_bh);
  2053. kfree(log_blocks);
  2054. kfree(real_blocks);
  2055. return -1;
  2056. }
  2057. }
  2058. /* read in the log blocks, memcpy to the corresponding real block */
  2059. ll_rw_block(REQ_OP_READ, 0, get_desc_trans_len(desc), log_blocks);
  2060. for (i = 0; i < get_desc_trans_len(desc); i++) {
  2061. wait_on_buffer(log_blocks[i]);
  2062. if (!buffer_uptodate(log_blocks[i])) {
  2063. reiserfs_warning(sb, "journal-1212",
  2064. "REPLAY FAILURE fsck required! "
  2065. "buffer write failed");
  2066. brelse_array(log_blocks + i,
  2067. get_desc_trans_len(desc) - i);
  2068. brelse_array(real_blocks, get_desc_trans_len(desc));
  2069. brelse(c_bh);
  2070. brelse(d_bh);
  2071. kfree(log_blocks);
  2072. kfree(real_blocks);
  2073. return -1;
  2074. }
  2075. memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data,
  2076. real_blocks[i]->b_size);
  2077. set_buffer_uptodate(real_blocks[i]);
  2078. brelse(log_blocks[i]);
  2079. }
  2080. /* flush out the real blocks */
  2081. for (i = 0; i < get_desc_trans_len(desc); i++) {
  2082. set_buffer_dirty(real_blocks[i]);
  2083. write_dirty_buffer(real_blocks[i], 0);
  2084. }
  2085. for (i = 0; i < get_desc_trans_len(desc); i++) {
  2086. wait_on_buffer(real_blocks[i]);
  2087. if (!buffer_uptodate(real_blocks[i])) {
  2088. reiserfs_warning(sb, "journal-1226",
  2089. "REPLAY FAILURE, fsck required! "
  2090. "buffer write failed");
  2091. brelse_array(real_blocks + i,
  2092. get_desc_trans_len(desc) - i);
  2093. brelse(c_bh);
  2094. brelse(d_bh);
  2095. kfree(log_blocks);
  2096. kfree(real_blocks);
  2097. return -1;
  2098. }
  2099. brelse(real_blocks[i]);
  2100. }
  2101. cur_dblock =
  2102. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2103. ((trans_offset + get_desc_trans_len(desc) +
  2104. 2) % SB_ONDISK_JOURNAL_SIZE(sb));
  2105. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2106. "journal-1095: setting journal " "start to offset %ld",
  2107. cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb));
  2108. /*
  2109. * init starting values for the first transaction, in case
  2110. * this is the last transaction to be replayed.
  2111. */
  2112. journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb);
  2113. journal->j_last_flush_trans_id = trans_id;
  2114. journal->j_trans_id = trans_id + 1;
  2115. /* check for trans_id overflow */
  2116. if (journal->j_trans_id == 0)
  2117. journal->j_trans_id = 10;
  2118. brelse(c_bh);
  2119. brelse(d_bh);
  2120. kfree(log_blocks);
  2121. kfree(real_blocks);
  2122. return 0;
  2123. }
  2124. /*
  2125. * This function reads blocks starting from block and to max_block of bufsize
  2126. * size (but no more than BUFNR blocks at a time). This proved to improve
  2127. * mounting speed on self-rebuilding raid5 arrays at least.
  2128. * Right now it is only used from journal code. But later we might use it
  2129. * from other places.
  2130. * Note: Do not use journal_getblk/sb_getblk functions here!
  2131. */
  2132. static struct buffer_head *reiserfs_breada(struct block_device *dev,
  2133. b_blocknr_t block, int bufsize,
  2134. b_blocknr_t max_block)
  2135. {
  2136. struct buffer_head *bhlist[BUFNR];
  2137. unsigned int blocks = BUFNR;
  2138. struct buffer_head *bh;
  2139. int i, j;
  2140. bh = __getblk(dev, block, bufsize);
  2141. if (buffer_uptodate(bh))
  2142. return (bh);
  2143. if (block + BUFNR > max_block) {
  2144. blocks = max_block - block;
  2145. }
  2146. bhlist[0] = bh;
  2147. j = 1;
  2148. for (i = 1; i < blocks; i++) {
  2149. bh = __getblk(dev, block + i, bufsize);
  2150. if (buffer_uptodate(bh)) {
  2151. brelse(bh);
  2152. break;
  2153. } else
  2154. bhlist[j++] = bh;
  2155. }
  2156. ll_rw_block(REQ_OP_READ, 0, j, bhlist);
  2157. for (i = 1; i < j; i++)
  2158. brelse(bhlist[i]);
  2159. bh = bhlist[0];
  2160. wait_on_buffer(bh);
  2161. if (buffer_uptodate(bh))
  2162. return bh;
  2163. brelse(bh);
  2164. return NULL;
  2165. }
  2166. /*
  2167. * read and replay the log
  2168. * on a clean unmount, the journal header's next unflushed pointer will be
  2169. * to an invalid transaction. This tests that before finding all the
  2170. * transactions in the log, which makes normal mount times fast.
  2171. *
  2172. * After a crash, this starts with the next unflushed transaction, and
  2173. * replays until it finds one too old, or invalid.
  2174. *
  2175. * On exit, it sets things up so the first transaction will work correctly.
  2176. * NOTE: only called during fs mount
  2177. */
  2178. static int journal_read(struct super_block *sb)
  2179. {
  2180. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  2181. struct reiserfs_journal_desc *desc;
  2182. unsigned int oldest_trans_id = 0;
  2183. unsigned int oldest_invalid_trans_id = 0;
  2184. time64_t start;
  2185. unsigned long oldest_start = 0;
  2186. unsigned long cur_dblock = 0;
  2187. unsigned long newest_mount_id = 9;
  2188. struct buffer_head *d_bh;
  2189. struct reiserfs_journal_header *jh;
  2190. int valid_journal_header = 0;
  2191. int replay_count = 0;
  2192. int continue_replay = 1;
  2193. int ret;
  2194. cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(sb);
  2195. reiserfs_info(sb, "checking transaction log (%pg)\n",
  2196. journal->j_dev_bd);
  2197. start = ktime_get_seconds();
  2198. /*
  2199. * step 1, read in the journal header block. Check the transaction
  2200. * it says is the first unflushed, and if that transaction is not
  2201. * valid, replay is done
  2202. */
  2203. journal->j_header_bh = journal_bread(sb,
  2204. SB_ONDISK_JOURNAL_1st_BLOCK(sb)
  2205. + SB_ONDISK_JOURNAL_SIZE(sb));
  2206. if (!journal->j_header_bh) {
  2207. return 1;
  2208. }
  2209. jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data);
  2210. if (le32_to_cpu(jh->j_first_unflushed_offset) <
  2211. SB_ONDISK_JOURNAL_SIZE(sb)
  2212. && le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
  2213. oldest_start =
  2214. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2215. le32_to_cpu(jh->j_first_unflushed_offset);
  2216. oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
  2217. newest_mount_id = le32_to_cpu(jh->j_mount_id);
  2218. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2219. "journal-1153: found in "
  2220. "header: first_unflushed_offset %d, last_flushed_trans_id "
  2221. "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
  2222. le32_to_cpu(jh->j_last_flush_trans_id));
  2223. valid_journal_header = 1;
  2224. /*
  2225. * now, we try to read the first unflushed offset. If it
  2226. * is not valid, there is nothing more we can do, and it
  2227. * makes no sense to read through the whole log.
  2228. */
  2229. d_bh =
  2230. journal_bread(sb,
  2231. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2232. le32_to_cpu(jh->j_first_unflushed_offset));
  2233. ret = journal_transaction_is_valid(sb, d_bh, NULL, NULL);
  2234. if (!ret) {
  2235. continue_replay = 0;
  2236. }
  2237. brelse(d_bh);
  2238. goto start_log_replay;
  2239. }
  2240. /*
  2241. * ok, there are transactions that need to be replayed. start
  2242. * with the first log block, find all the valid transactions, and
  2243. * pick out the oldest.
  2244. */
  2245. while (continue_replay
  2246. && cur_dblock <
  2247. (SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2248. SB_ONDISK_JOURNAL_SIZE(sb))) {
  2249. /*
  2250. * Note that it is required for blocksize of primary fs
  2251. * device and journal device to be the same
  2252. */
  2253. d_bh =
  2254. reiserfs_breada(journal->j_dev_bd, cur_dblock,
  2255. sb->s_blocksize,
  2256. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2257. SB_ONDISK_JOURNAL_SIZE(sb));
  2258. ret =
  2259. journal_transaction_is_valid(sb, d_bh,
  2260. &oldest_invalid_trans_id,
  2261. &newest_mount_id);
  2262. if (ret == 1) {
  2263. desc = (struct reiserfs_journal_desc *)d_bh->b_data;
  2264. if (oldest_start == 0) { /* init all oldest_ values */
  2265. oldest_trans_id = get_desc_trans_id(desc);
  2266. oldest_start = d_bh->b_blocknr;
  2267. newest_mount_id = get_desc_mount_id(desc);
  2268. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2269. "journal-1179: Setting "
  2270. "oldest_start to offset %llu, trans_id %lu",
  2271. oldest_start -
  2272. SB_ONDISK_JOURNAL_1st_BLOCK
  2273. (sb), oldest_trans_id);
  2274. } else if (oldest_trans_id > get_desc_trans_id(desc)) {
  2275. /* one we just read was older */
  2276. oldest_trans_id = get_desc_trans_id(desc);
  2277. oldest_start = d_bh->b_blocknr;
  2278. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2279. "journal-1180: Resetting "
  2280. "oldest_start to offset %lu, trans_id %lu",
  2281. oldest_start -
  2282. SB_ONDISK_JOURNAL_1st_BLOCK
  2283. (sb), oldest_trans_id);
  2284. }
  2285. if (newest_mount_id < get_desc_mount_id(desc)) {
  2286. newest_mount_id = get_desc_mount_id(desc);
  2287. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2288. "journal-1299: Setting "
  2289. "newest_mount_id to %d",
  2290. get_desc_mount_id(desc));
  2291. }
  2292. cur_dblock += get_desc_trans_len(desc) + 2;
  2293. } else {
  2294. cur_dblock++;
  2295. }
  2296. brelse(d_bh);
  2297. }
  2298. start_log_replay:
  2299. cur_dblock = oldest_start;
  2300. if (oldest_trans_id) {
  2301. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2302. "journal-1206: Starting replay "
  2303. "from offset %llu, trans_id %lu",
  2304. cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb),
  2305. oldest_trans_id);
  2306. }
  2307. replay_count = 0;
  2308. while (continue_replay && oldest_trans_id > 0) {
  2309. ret =
  2310. journal_read_transaction(sb, cur_dblock, oldest_start,
  2311. oldest_trans_id, newest_mount_id);
  2312. if (ret < 0) {
  2313. return ret;
  2314. } else if (ret != 0) {
  2315. break;
  2316. }
  2317. cur_dblock =
  2318. SB_ONDISK_JOURNAL_1st_BLOCK(sb) + journal->j_start;
  2319. replay_count++;
  2320. if (cur_dblock == oldest_start)
  2321. break;
  2322. }
  2323. if (oldest_trans_id == 0) {
  2324. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  2325. "journal-1225: No valid " "transactions found");
  2326. }
  2327. /*
  2328. * j_start does not get set correctly if we don't replay any
  2329. * transactions. if we had a valid journal_header, set j_start
  2330. * to the first unflushed transaction value, copy the trans_id
  2331. * from the header
  2332. */
  2333. if (valid_journal_header && replay_count == 0) {
  2334. journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset);
  2335. journal->j_trans_id =
  2336. le32_to_cpu(jh->j_last_flush_trans_id) + 1;
  2337. /* check for trans_id overflow */
  2338. if (journal->j_trans_id == 0)
  2339. journal->j_trans_id = 10;
  2340. journal->j_last_flush_trans_id =
  2341. le32_to_cpu(jh->j_last_flush_trans_id);
  2342. journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
  2343. } else {
  2344. journal->j_mount_id = newest_mount_id + 1;
  2345. }
  2346. reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
  2347. "newest_mount_id to %lu", journal->j_mount_id);
  2348. journal->j_first_unflushed_offset = journal->j_start;
  2349. if (replay_count > 0) {
  2350. reiserfs_info(sb,
  2351. "replayed %d transactions in %lu seconds\n",
  2352. replay_count, ktime_get_seconds() - start);
  2353. }
  2354. /* needed to satisfy the locking in _update_journal_header_block */
  2355. reiserfs_write_lock(sb);
  2356. if (!bdev_read_only(sb->s_bdev) &&
  2357. _update_journal_header_block(sb, journal->j_start,
  2358. journal->j_last_flush_trans_id)) {
  2359. reiserfs_write_unlock(sb);
  2360. /*
  2361. * replay failed, caller must call free_journal_ram and abort
  2362. * the mount
  2363. */
  2364. return -1;
  2365. }
  2366. reiserfs_write_unlock(sb);
  2367. return 0;
  2368. }
  2369. static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
  2370. {
  2371. struct reiserfs_journal_list *jl;
  2372. jl = kzalloc(sizeof(struct reiserfs_journal_list),
  2373. GFP_NOFS | __GFP_NOFAIL);
  2374. INIT_LIST_HEAD(&jl->j_list);
  2375. INIT_LIST_HEAD(&jl->j_working_list);
  2376. INIT_LIST_HEAD(&jl->j_tail_bh_list);
  2377. INIT_LIST_HEAD(&jl->j_bh_list);
  2378. mutex_init(&jl->j_commit_mutex);
  2379. SB_JOURNAL(s)->j_num_lists++;
  2380. get_journal_list(jl);
  2381. return jl;
  2382. }
  2383. static void journal_list_init(struct super_block *sb)
  2384. {
  2385. SB_JOURNAL(sb)->j_current_jl = alloc_journal_list(sb);
  2386. }
  2387. static void release_journal_dev(struct super_block *super,
  2388. struct reiserfs_journal *journal)
  2389. {
  2390. if (journal->j_dev_bd != NULL) {
  2391. blkdev_put(journal->j_dev_bd, journal->j_dev_mode);
  2392. journal->j_dev_bd = NULL;
  2393. }
  2394. }
  2395. static int journal_init_dev(struct super_block *super,
  2396. struct reiserfs_journal *journal,
  2397. const char *jdev_name)
  2398. {
  2399. int result;
  2400. dev_t jdev;
  2401. fmode_t blkdev_mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
  2402. char b[BDEVNAME_SIZE];
  2403. result = 0;
  2404. journal->j_dev_bd = NULL;
  2405. jdev = SB_ONDISK_JOURNAL_DEVICE(super) ?
  2406. new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
  2407. if (bdev_read_only(super->s_bdev))
  2408. blkdev_mode = FMODE_READ;
  2409. /* there is no "jdev" option and journal is on separate device */
  2410. if ((!jdev_name || !jdev_name[0])) {
  2411. if (jdev == super->s_dev)
  2412. blkdev_mode &= ~FMODE_EXCL;
  2413. journal->j_dev_bd = blkdev_get_by_dev(jdev, blkdev_mode,
  2414. journal);
  2415. journal->j_dev_mode = blkdev_mode;
  2416. if (IS_ERR(journal->j_dev_bd)) {
  2417. result = PTR_ERR(journal->j_dev_bd);
  2418. journal->j_dev_bd = NULL;
  2419. reiserfs_warning(super, "sh-458",
  2420. "cannot init journal device '%s': %i",
  2421. __bdevname(jdev, b), result);
  2422. return result;
  2423. } else if (jdev != super->s_dev)
  2424. set_blocksize(journal->j_dev_bd, super->s_blocksize);
  2425. return 0;
  2426. }
  2427. journal->j_dev_mode = blkdev_mode;
  2428. journal->j_dev_bd = blkdev_get_by_path(jdev_name, blkdev_mode, journal);
  2429. if (IS_ERR(journal->j_dev_bd)) {
  2430. result = PTR_ERR(journal->j_dev_bd);
  2431. journal->j_dev_bd = NULL;
  2432. reiserfs_warning(super, "sh-457",
  2433. "journal_init_dev: Cannot open '%s': %i",
  2434. jdev_name, result);
  2435. return result;
  2436. }
  2437. set_blocksize(journal->j_dev_bd, super->s_blocksize);
  2438. reiserfs_info(super,
  2439. "journal_init_dev: journal device: %pg\n",
  2440. journal->j_dev_bd);
  2441. return 0;
  2442. }
  2443. /*
  2444. * When creating/tuning a file system user can assign some
  2445. * journal params within boundaries which depend on the ratio
  2446. * blocksize/standard_blocksize.
  2447. *
  2448. * For blocks >= standard_blocksize transaction size should
  2449. * be not less then JOURNAL_TRANS_MIN_DEFAULT, and not more
  2450. * then JOURNAL_TRANS_MAX_DEFAULT.
  2451. *
  2452. * For blocks < standard_blocksize these boundaries should be
  2453. * decreased proportionally.
  2454. */
  2455. #define REISERFS_STANDARD_BLKSIZE (4096)
  2456. static int check_advise_trans_params(struct super_block *sb,
  2457. struct reiserfs_journal *journal)
  2458. {
  2459. if (journal->j_trans_max) {
  2460. /* Non-default journal params. Do sanity check for them. */
  2461. int ratio = 1;
  2462. if (sb->s_blocksize < REISERFS_STANDARD_BLKSIZE)
  2463. ratio = REISERFS_STANDARD_BLKSIZE / sb->s_blocksize;
  2464. if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio ||
  2465. journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio ||
  2466. SB_ONDISK_JOURNAL_SIZE(sb) / journal->j_trans_max <
  2467. JOURNAL_MIN_RATIO) {
  2468. reiserfs_warning(sb, "sh-462",
  2469. "bad transaction max size (%u). "
  2470. "FSCK?", journal->j_trans_max);
  2471. return 1;
  2472. }
  2473. if (journal->j_max_batch != (journal->j_trans_max) *
  2474. JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT) {
  2475. reiserfs_warning(sb, "sh-463",
  2476. "bad transaction max batch (%u). "
  2477. "FSCK?", journal->j_max_batch);
  2478. return 1;
  2479. }
  2480. } else {
  2481. /*
  2482. * Default journal params.
  2483. * The file system was created by old version
  2484. * of mkreiserfs, so some fields contain zeros,
  2485. * and we need to advise proper values for them
  2486. */
  2487. if (sb->s_blocksize != REISERFS_STANDARD_BLKSIZE) {
  2488. reiserfs_warning(sb, "sh-464", "bad blocksize (%u)",
  2489. sb->s_blocksize);
  2490. return 1;
  2491. }
  2492. journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT;
  2493. journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT;
  2494. journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE;
  2495. }
  2496. return 0;
  2497. }
  2498. /* must be called once on fs mount. calls journal_read for you */
  2499. int journal_init(struct super_block *sb, const char *j_dev_name,
  2500. int old_format, unsigned int commit_max_age)
  2501. {
  2502. int num_cnodes = SB_ONDISK_JOURNAL_SIZE(sb) * 2;
  2503. struct buffer_head *bhjh;
  2504. struct reiserfs_super_block *rs;
  2505. struct reiserfs_journal_header *jh;
  2506. struct reiserfs_journal *journal;
  2507. struct reiserfs_journal_list *jl;
  2508. int ret;
  2509. journal = SB_JOURNAL(sb) = vzalloc(sizeof(struct reiserfs_journal));
  2510. if (!journal) {
  2511. reiserfs_warning(sb, "journal-1256",
  2512. "unable to get memory for journal structure");
  2513. return 1;
  2514. }
  2515. INIT_LIST_HEAD(&journal->j_bitmap_nodes);
  2516. INIT_LIST_HEAD(&journal->j_prealloc_list);
  2517. INIT_LIST_HEAD(&journal->j_working_list);
  2518. INIT_LIST_HEAD(&journal->j_journal_list);
  2519. journal->j_persistent_trans = 0;
  2520. if (reiserfs_allocate_list_bitmaps(sb, journal->j_list_bitmap,
  2521. reiserfs_bmap_count(sb)))
  2522. goto free_and_return;
  2523. allocate_bitmap_nodes(sb);
  2524. /* reserved for journal area support */
  2525. SB_JOURNAL_1st_RESERVED_BLOCK(sb) = (old_format ?
  2526. REISERFS_OLD_DISK_OFFSET_IN_BYTES
  2527. / sb->s_blocksize +
  2528. reiserfs_bmap_count(sb) +
  2529. 1 :
  2530. REISERFS_DISK_OFFSET_IN_BYTES /
  2531. sb->s_blocksize + 2);
  2532. /*
  2533. * Sanity check to see is the standard journal fitting
  2534. * within first bitmap (actual for small blocksizes)
  2535. */
  2536. if (!SB_ONDISK_JOURNAL_DEVICE(sb) &&
  2537. (SB_JOURNAL_1st_RESERVED_BLOCK(sb) +
  2538. SB_ONDISK_JOURNAL_SIZE(sb) > sb->s_blocksize * 8)) {
  2539. reiserfs_warning(sb, "journal-1393",
  2540. "journal does not fit for area addressed "
  2541. "by first of bitmap blocks. It starts at "
  2542. "%u and its size is %u. Block size %ld",
  2543. SB_JOURNAL_1st_RESERVED_BLOCK(sb),
  2544. SB_ONDISK_JOURNAL_SIZE(sb),
  2545. sb->s_blocksize);
  2546. goto free_and_return;
  2547. }
  2548. if (journal_init_dev(sb, journal, j_dev_name) != 0) {
  2549. reiserfs_warning(sb, "sh-462",
  2550. "unable to initialize journal device");
  2551. goto free_and_return;
  2552. }
  2553. rs = SB_DISK_SUPER_BLOCK(sb);
  2554. /* read journal header */
  2555. bhjh = journal_bread(sb,
  2556. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  2557. SB_ONDISK_JOURNAL_SIZE(sb));
  2558. if (!bhjh) {
  2559. reiserfs_warning(sb, "sh-459",
  2560. "unable to read journal header");
  2561. goto free_and_return;
  2562. }
  2563. jh = (struct reiserfs_journal_header *)(bhjh->b_data);
  2564. /* make sure that journal matches to the super block */
  2565. if (is_reiserfs_jr(rs)
  2566. && (le32_to_cpu(jh->jh_journal.jp_journal_magic) !=
  2567. sb_jp_journal_magic(rs))) {
  2568. reiserfs_warning(sb, "sh-460",
  2569. "journal header magic %x (device %pg) does "
  2570. "not match to magic found in super block %x",
  2571. jh->jh_journal.jp_journal_magic,
  2572. journal->j_dev_bd,
  2573. sb_jp_journal_magic(rs));
  2574. brelse(bhjh);
  2575. goto free_and_return;
  2576. }
  2577. journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max);
  2578. journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch);
  2579. journal->j_max_commit_age =
  2580. le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age);
  2581. journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
  2582. if (check_advise_trans_params(sb, journal) != 0)
  2583. goto free_and_return;
  2584. journal->j_default_max_commit_age = journal->j_max_commit_age;
  2585. if (commit_max_age != 0) {
  2586. journal->j_max_commit_age = commit_max_age;
  2587. journal->j_max_trans_age = commit_max_age;
  2588. }
  2589. reiserfs_info(sb, "journal params: device %pg, size %u, "
  2590. "journal first block %u, max trans len %u, max batch %u, "
  2591. "max commit age %u, max trans age %u\n",
  2592. journal->j_dev_bd,
  2593. SB_ONDISK_JOURNAL_SIZE(sb),
  2594. SB_ONDISK_JOURNAL_1st_BLOCK(sb),
  2595. journal->j_trans_max,
  2596. journal->j_max_batch,
  2597. journal->j_max_commit_age, journal->j_max_trans_age);
  2598. brelse(bhjh);
  2599. journal->j_list_bitmap_index = 0;
  2600. journal_list_init(sb);
  2601. memset(journal->j_list_hash_table, 0,
  2602. JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
  2603. INIT_LIST_HEAD(&journal->j_dirty_buffers);
  2604. spin_lock_init(&journal->j_dirty_buffers_lock);
  2605. journal->j_start = 0;
  2606. journal->j_len = 0;
  2607. journal->j_len_alloc = 0;
  2608. atomic_set(&journal->j_wcount, 0);
  2609. atomic_set(&journal->j_async_throttle, 0);
  2610. journal->j_bcount = 0;
  2611. journal->j_trans_start_time = 0;
  2612. journal->j_last = NULL;
  2613. journal->j_first = NULL;
  2614. init_waitqueue_head(&journal->j_join_wait);
  2615. mutex_init(&journal->j_mutex);
  2616. mutex_init(&journal->j_flush_mutex);
  2617. journal->j_trans_id = 10;
  2618. journal->j_mount_id = 10;
  2619. journal->j_state = 0;
  2620. atomic_set(&journal->j_jlock, 0);
  2621. journal->j_cnode_free_list = allocate_cnodes(num_cnodes);
  2622. journal->j_cnode_free_orig = journal->j_cnode_free_list;
  2623. journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0;
  2624. journal->j_cnode_used = 0;
  2625. journal->j_must_wait = 0;
  2626. if (journal->j_cnode_free == 0) {
  2627. reiserfs_warning(sb, "journal-2004", "Journal cnode memory "
  2628. "allocation failed (%ld bytes). Journal is "
  2629. "too large for available memory. Usually "
  2630. "this is due to a journal that is too large.",
  2631. sizeof (struct reiserfs_journal_cnode) * num_cnodes);
  2632. goto free_and_return;
  2633. }
  2634. init_journal_hash(sb);
  2635. jl = journal->j_current_jl;
  2636. /*
  2637. * get_list_bitmap() may call flush_commit_list() which
  2638. * requires the lock. Calling flush_commit_list() shouldn't happen
  2639. * this early but I like to be paranoid.
  2640. */
  2641. reiserfs_write_lock(sb);
  2642. jl->j_list_bitmap = get_list_bitmap(sb, jl);
  2643. reiserfs_write_unlock(sb);
  2644. if (!jl->j_list_bitmap) {
  2645. reiserfs_warning(sb, "journal-2005",
  2646. "get_list_bitmap failed for journal list 0");
  2647. goto free_and_return;
  2648. }
  2649. ret = journal_read(sb);
  2650. if (ret < 0) {
  2651. reiserfs_warning(sb, "reiserfs-2006",
  2652. "Replay Failure, unable to mount");
  2653. goto free_and_return;
  2654. }
  2655. INIT_DELAYED_WORK(&journal->j_work, flush_async_commits);
  2656. journal->j_work_sb = sb;
  2657. return 0;
  2658. free_and_return:
  2659. free_journal_ram(sb);
  2660. return 1;
  2661. }
  2662. /*
  2663. * test for a polite end of the current transaction. Used by file_write,
  2664. * and should be used by delete to make sure they don't write more than
  2665. * can fit inside a single transaction
  2666. */
  2667. int journal_transaction_should_end(struct reiserfs_transaction_handle *th,
  2668. int new_alloc)
  2669. {
  2670. struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
  2671. time64_t now = ktime_get_seconds();
  2672. /* cannot restart while nested */
  2673. BUG_ON(!th->t_trans_id);
  2674. if (th->t_refcount > 1)
  2675. return 0;
  2676. if (journal->j_must_wait > 0 ||
  2677. (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
  2678. atomic_read(&journal->j_jlock) ||
  2679. (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
  2680. journal->j_cnode_free < (journal->j_trans_max * 3)) {
  2681. return 1;
  2682. }
  2683. journal->j_len_alloc += new_alloc;
  2684. th->t_blocks_allocated += new_alloc ;
  2685. return 0;
  2686. }
  2687. /* this must be called inside a transaction */
  2688. void reiserfs_block_writes(struct reiserfs_transaction_handle *th)
  2689. {
  2690. struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
  2691. BUG_ON(!th->t_trans_id);
  2692. journal->j_must_wait = 1;
  2693. set_bit(J_WRITERS_BLOCKED, &journal->j_state);
  2694. return;
  2695. }
  2696. /* this must be called without a transaction started */
  2697. void reiserfs_allow_writes(struct super_block *s)
  2698. {
  2699. struct reiserfs_journal *journal = SB_JOURNAL(s);
  2700. clear_bit(J_WRITERS_BLOCKED, &journal->j_state);
  2701. wake_up(&journal->j_join_wait);
  2702. }
  2703. /* this must be called without a transaction started */
  2704. void reiserfs_wait_on_write_block(struct super_block *s)
  2705. {
  2706. struct reiserfs_journal *journal = SB_JOURNAL(s);
  2707. wait_event(journal->j_join_wait,
  2708. !test_bit(J_WRITERS_BLOCKED, &journal->j_state));
  2709. }
  2710. static void queue_log_writer(struct super_block *s)
  2711. {
  2712. wait_queue_entry_t wait;
  2713. struct reiserfs_journal *journal = SB_JOURNAL(s);
  2714. set_bit(J_WRITERS_QUEUED, &journal->j_state);
  2715. /*
  2716. * we don't want to use wait_event here because
  2717. * we only want to wait once.
  2718. */
  2719. init_waitqueue_entry(&wait, current);
  2720. add_wait_queue(&journal->j_join_wait, &wait);
  2721. set_current_state(TASK_UNINTERRUPTIBLE);
  2722. if (test_bit(J_WRITERS_QUEUED, &journal->j_state)) {
  2723. int depth = reiserfs_write_unlock_nested(s);
  2724. schedule();
  2725. reiserfs_write_lock_nested(s, depth);
  2726. }
  2727. __set_current_state(TASK_RUNNING);
  2728. remove_wait_queue(&journal->j_join_wait, &wait);
  2729. }
  2730. static void wake_queued_writers(struct super_block *s)
  2731. {
  2732. struct reiserfs_journal *journal = SB_JOURNAL(s);
  2733. if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
  2734. wake_up(&journal->j_join_wait);
  2735. }
  2736. static void let_transaction_grow(struct super_block *sb, unsigned int trans_id)
  2737. {
  2738. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  2739. unsigned long bcount = journal->j_bcount;
  2740. while (1) {
  2741. int depth;
  2742. depth = reiserfs_write_unlock_nested(sb);
  2743. schedule_timeout_uninterruptible(1);
  2744. reiserfs_write_lock_nested(sb, depth);
  2745. journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
  2746. while ((atomic_read(&journal->j_wcount) > 0 ||
  2747. atomic_read(&journal->j_jlock)) &&
  2748. journal->j_trans_id == trans_id) {
  2749. queue_log_writer(sb);
  2750. }
  2751. if (journal->j_trans_id != trans_id)
  2752. break;
  2753. if (bcount == journal->j_bcount)
  2754. break;
  2755. bcount = journal->j_bcount;
  2756. }
  2757. }
  2758. /*
  2759. * join == true if you must join an existing transaction.
  2760. * join == false if you can deal with waiting for others to finish
  2761. *
  2762. * this will block until the transaction is joinable. send the number of
  2763. * blocks you expect to use in nblocks.
  2764. */
  2765. static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
  2766. struct super_block *sb, unsigned long nblocks,
  2767. int join)
  2768. {
  2769. time64_t now = ktime_get_seconds();
  2770. unsigned int old_trans_id;
  2771. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  2772. struct reiserfs_transaction_handle myth;
  2773. int sched_count = 0;
  2774. int retval;
  2775. int depth;
  2776. reiserfs_check_lock_depth(sb, "journal_begin");
  2777. BUG_ON(nblocks > journal->j_trans_max);
  2778. PROC_INFO_INC(sb, journal.journal_being);
  2779. /* set here for journal_join */
  2780. th->t_refcount = 1;
  2781. th->t_super = sb;
  2782. relock:
  2783. lock_journal(sb);
  2784. if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) {
  2785. unlock_journal(sb);
  2786. retval = journal->j_errno;
  2787. goto out_fail;
  2788. }
  2789. journal->j_bcount++;
  2790. if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
  2791. unlock_journal(sb);
  2792. depth = reiserfs_write_unlock_nested(sb);
  2793. reiserfs_wait_on_write_block(sb);
  2794. reiserfs_write_lock_nested(sb, depth);
  2795. PROC_INFO_INC(sb, journal.journal_relock_writers);
  2796. goto relock;
  2797. }
  2798. now = ktime_get_seconds();
  2799. /*
  2800. * if there is no room in the journal OR
  2801. * if this transaction is too old, and we weren't called joinable,
  2802. * wait for it to finish before beginning we don't sleep if there
  2803. * aren't other writers
  2804. */
  2805. if ((!join && journal->j_must_wait > 0) ||
  2806. (!join
  2807. && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch)
  2808. || (!join && atomic_read(&journal->j_wcount) > 0
  2809. && journal->j_trans_start_time > 0
  2810. && (now - journal->j_trans_start_time) >
  2811. journal->j_max_trans_age) || (!join
  2812. && atomic_read(&journal->j_jlock))
  2813. || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
  2814. old_trans_id = journal->j_trans_id;
  2815. /* allow others to finish this transaction */
  2816. unlock_journal(sb);
  2817. if (!join && (journal->j_len_alloc + nblocks + 2) >=
  2818. journal->j_max_batch &&
  2819. ((journal->j_len + nblocks + 2) * 100) <
  2820. (journal->j_len_alloc * 75)) {
  2821. if (atomic_read(&journal->j_wcount) > 10) {
  2822. sched_count++;
  2823. queue_log_writer(sb);
  2824. goto relock;
  2825. }
  2826. }
  2827. /*
  2828. * don't mess with joining the transaction if all we
  2829. * have to do is wait for someone else to do a commit
  2830. */
  2831. if (atomic_read(&journal->j_jlock)) {
  2832. while (journal->j_trans_id == old_trans_id &&
  2833. atomic_read(&journal->j_jlock)) {
  2834. queue_log_writer(sb);
  2835. }
  2836. goto relock;
  2837. }
  2838. retval = journal_join(&myth, sb);
  2839. if (retval)
  2840. goto out_fail;
  2841. /* someone might have ended the transaction while we joined */
  2842. if (old_trans_id != journal->j_trans_id) {
  2843. retval = do_journal_end(&myth, 0);
  2844. } else {
  2845. retval = do_journal_end(&myth, COMMIT_NOW);
  2846. }
  2847. if (retval)
  2848. goto out_fail;
  2849. PROC_INFO_INC(sb, journal.journal_relock_wcount);
  2850. goto relock;
  2851. }
  2852. /* we are the first writer, set trans_id */
  2853. if (journal->j_trans_start_time == 0) {
  2854. journal->j_trans_start_time = ktime_get_seconds();
  2855. }
  2856. atomic_inc(&journal->j_wcount);
  2857. journal->j_len_alloc += nblocks;
  2858. th->t_blocks_logged = 0;
  2859. th->t_blocks_allocated = nblocks;
  2860. th->t_trans_id = journal->j_trans_id;
  2861. unlock_journal(sb);
  2862. INIT_LIST_HEAD(&th->t_list);
  2863. return 0;
  2864. out_fail:
  2865. memset(th, 0, sizeof(*th));
  2866. /*
  2867. * Re-set th->t_super, so we can properly keep track of how many
  2868. * persistent transactions there are. We need to do this so if this
  2869. * call is part of a failed restart_transaction, we can free it later
  2870. */
  2871. th->t_super = sb;
  2872. return retval;
  2873. }
  2874. struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct
  2875. super_block
  2876. *s,
  2877. int nblocks)
  2878. {
  2879. int ret;
  2880. struct reiserfs_transaction_handle *th;
  2881. /*
  2882. * if we're nesting into an existing transaction. It will be
  2883. * persistent on its own
  2884. */
  2885. if (reiserfs_transaction_running(s)) {
  2886. th = current->journal_info;
  2887. th->t_refcount++;
  2888. BUG_ON(th->t_refcount < 2);
  2889. return th;
  2890. }
  2891. th = kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS);
  2892. if (!th)
  2893. return NULL;
  2894. ret = journal_begin(th, s, nblocks);
  2895. if (ret) {
  2896. kfree(th);
  2897. return NULL;
  2898. }
  2899. SB_JOURNAL(s)->j_persistent_trans++;
  2900. return th;
  2901. }
  2902. int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th)
  2903. {
  2904. struct super_block *s = th->t_super;
  2905. int ret = 0;
  2906. if (th->t_trans_id)
  2907. ret = journal_end(th);
  2908. else
  2909. ret = -EIO;
  2910. if (th->t_refcount == 0) {
  2911. SB_JOURNAL(s)->j_persistent_trans--;
  2912. kfree(th);
  2913. }
  2914. return ret;
  2915. }
  2916. static int journal_join(struct reiserfs_transaction_handle *th,
  2917. struct super_block *sb)
  2918. {
  2919. struct reiserfs_transaction_handle *cur_th = current->journal_info;
  2920. /*
  2921. * this keeps do_journal_end from NULLing out the
  2922. * current->journal_info pointer
  2923. */
  2924. th->t_handle_save = cur_th;
  2925. BUG_ON(cur_th && cur_th->t_refcount > 1);
  2926. return do_journal_begin_r(th, sb, 1, JBEGIN_JOIN);
  2927. }
  2928. int journal_join_abort(struct reiserfs_transaction_handle *th,
  2929. struct super_block *sb)
  2930. {
  2931. struct reiserfs_transaction_handle *cur_th = current->journal_info;
  2932. /*
  2933. * this keeps do_journal_end from NULLing out the
  2934. * current->journal_info pointer
  2935. */
  2936. th->t_handle_save = cur_th;
  2937. BUG_ON(cur_th && cur_th->t_refcount > 1);
  2938. return do_journal_begin_r(th, sb, 1, JBEGIN_ABORT);
  2939. }
  2940. int journal_begin(struct reiserfs_transaction_handle *th,
  2941. struct super_block *sb, unsigned long nblocks)
  2942. {
  2943. struct reiserfs_transaction_handle *cur_th = current->journal_info;
  2944. int ret;
  2945. th->t_handle_save = NULL;
  2946. if (cur_th) {
  2947. /* we are nesting into the current transaction */
  2948. if (cur_th->t_super == sb) {
  2949. BUG_ON(!cur_th->t_refcount);
  2950. cur_th->t_refcount++;
  2951. memcpy(th, cur_th, sizeof(*th));
  2952. if (th->t_refcount <= 1)
  2953. reiserfs_warning(sb, "reiserfs-2005",
  2954. "BAD: refcount <= 1, but "
  2955. "journal_info != 0");
  2956. return 0;
  2957. } else {
  2958. /*
  2959. * we've ended up with a handle from a different
  2960. * filesystem. save it and restore on journal_end.
  2961. * This should never really happen...
  2962. */
  2963. reiserfs_warning(sb, "clm-2100",
  2964. "nesting info a different FS");
  2965. th->t_handle_save = current->journal_info;
  2966. current->journal_info = th;
  2967. }
  2968. } else {
  2969. current->journal_info = th;
  2970. }
  2971. ret = do_journal_begin_r(th, sb, nblocks, JBEGIN_REG);
  2972. BUG_ON(current->journal_info != th);
  2973. /*
  2974. * I guess this boils down to being the reciprocal of clm-2100 above.
  2975. * If do_journal_begin_r fails, we need to put it back, since
  2976. * journal_end won't be called to do it. */
  2977. if (ret)
  2978. current->journal_info = th->t_handle_save;
  2979. else
  2980. BUG_ON(!th->t_refcount);
  2981. return ret;
  2982. }
  2983. /*
  2984. * puts bh into the current transaction. If it was already there, reorders
  2985. * removes the old pointers from the hash, and puts new ones in (to make
  2986. * sure replay happen in the right order).
  2987. *
  2988. * if it was dirty, cleans and files onto the clean list. I can't let it
  2989. * be dirty again until the transaction is committed.
  2990. *
  2991. * if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
  2992. */
  2993. int journal_mark_dirty(struct reiserfs_transaction_handle *th,
  2994. struct buffer_head *bh)
  2995. {
  2996. struct super_block *sb = th->t_super;
  2997. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  2998. struct reiserfs_journal_cnode *cn = NULL;
  2999. int count_already_incd = 0;
  3000. int prepared = 0;
  3001. BUG_ON(!th->t_trans_id);
  3002. PROC_INFO_INC(sb, journal.mark_dirty);
  3003. if (th->t_trans_id != journal->j_trans_id) {
  3004. reiserfs_panic(th->t_super, "journal-1577",
  3005. "handle trans id %ld != current trans id %ld",
  3006. th->t_trans_id, journal->j_trans_id);
  3007. }
  3008. prepared = test_clear_buffer_journal_prepared(bh);
  3009. clear_buffer_journal_restore_dirty(bh);
  3010. /* already in this transaction, we are done */
  3011. if (buffer_journaled(bh)) {
  3012. PROC_INFO_INC(sb, journal.mark_dirty_already);
  3013. return 0;
  3014. }
  3015. /*
  3016. * this must be turned into a panic instead of a warning. We can't
  3017. * allow a dirty or journal_dirty or locked buffer to be logged, as
  3018. * some changes could get to disk too early. NOT GOOD.
  3019. */
  3020. if (!prepared || buffer_dirty(bh)) {
  3021. reiserfs_warning(sb, "journal-1777",
  3022. "buffer %llu bad state "
  3023. "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
  3024. (unsigned long long)bh->b_blocknr,
  3025. prepared ? ' ' : '!',
  3026. buffer_locked(bh) ? ' ' : '!',
  3027. buffer_dirty(bh) ? ' ' : '!',
  3028. buffer_journal_dirty(bh) ? ' ' : '!');
  3029. }
  3030. if (atomic_read(&journal->j_wcount) <= 0) {
  3031. reiserfs_warning(sb, "journal-1409",
  3032. "returning because j_wcount was %d",
  3033. atomic_read(&journal->j_wcount));
  3034. return 1;
  3035. }
  3036. /*
  3037. * this error means I've screwed up, and we've overflowed
  3038. * the transaction. Nothing can be done here, except make the
  3039. * FS readonly or panic.
  3040. */
  3041. if (journal->j_len >= journal->j_trans_max) {
  3042. reiserfs_panic(th->t_super, "journal-1413",
  3043. "j_len (%lu) is too big",
  3044. journal->j_len);
  3045. }
  3046. if (buffer_journal_dirty(bh)) {
  3047. count_already_incd = 1;
  3048. PROC_INFO_INC(sb, journal.mark_dirty_notjournal);
  3049. clear_buffer_journal_dirty(bh);
  3050. }
  3051. if (journal->j_len > journal->j_len_alloc) {
  3052. journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT;
  3053. }
  3054. set_buffer_journaled(bh);
  3055. /* now put this guy on the end */
  3056. if (!cn) {
  3057. cn = get_cnode(sb);
  3058. if (!cn) {
  3059. reiserfs_panic(sb, "journal-4", "get_cnode failed!");
  3060. }
  3061. if (th->t_blocks_logged == th->t_blocks_allocated) {
  3062. th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT;
  3063. journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT;
  3064. }
  3065. th->t_blocks_logged++;
  3066. journal->j_len++;
  3067. cn->bh = bh;
  3068. cn->blocknr = bh->b_blocknr;
  3069. cn->sb = sb;
  3070. cn->jlist = NULL;
  3071. insert_journal_hash(journal->j_hash_table, cn);
  3072. if (!count_already_incd) {
  3073. get_bh(bh);
  3074. }
  3075. }
  3076. cn->next = NULL;
  3077. cn->prev = journal->j_last;
  3078. cn->bh = bh;
  3079. if (journal->j_last) {
  3080. journal->j_last->next = cn;
  3081. journal->j_last = cn;
  3082. } else {
  3083. journal->j_first = cn;
  3084. journal->j_last = cn;
  3085. }
  3086. reiserfs_schedule_old_flush(sb);
  3087. return 0;
  3088. }
  3089. int journal_end(struct reiserfs_transaction_handle *th)
  3090. {
  3091. struct super_block *sb = th->t_super;
  3092. if (!current->journal_info && th->t_refcount > 1)
  3093. reiserfs_warning(sb, "REISER-NESTING",
  3094. "th NULL, refcount %d", th->t_refcount);
  3095. if (!th->t_trans_id) {
  3096. WARN_ON(1);
  3097. return -EIO;
  3098. }
  3099. th->t_refcount--;
  3100. if (th->t_refcount > 0) {
  3101. struct reiserfs_transaction_handle *cur_th =
  3102. current->journal_info;
  3103. /*
  3104. * we aren't allowed to close a nested transaction on a
  3105. * different filesystem from the one in the task struct
  3106. */
  3107. BUG_ON(cur_th->t_super != th->t_super);
  3108. if (th != cur_th) {
  3109. memcpy(current->journal_info, th, sizeof(*th));
  3110. th->t_trans_id = 0;
  3111. }
  3112. return 0;
  3113. } else {
  3114. return do_journal_end(th, 0);
  3115. }
  3116. }
  3117. /*
  3118. * removes from the current transaction, relsing and descrementing any counters.
  3119. * also files the removed buffer directly onto the clean list
  3120. *
  3121. * called by journal_mark_freed when a block has been deleted
  3122. *
  3123. * returns 1 if it cleaned and relsed the buffer. 0 otherwise
  3124. */
  3125. static int remove_from_transaction(struct super_block *sb,
  3126. b_blocknr_t blocknr, int already_cleaned)
  3127. {
  3128. struct buffer_head *bh;
  3129. struct reiserfs_journal_cnode *cn;
  3130. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3131. int ret = 0;
  3132. cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr);
  3133. if (!cn || !cn->bh) {
  3134. return ret;
  3135. }
  3136. bh = cn->bh;
  3137. if (cn->prev) {
  3138. cn->prev->next = cn->next;
  3139. }
  3140. if (cn->next) {
  3141. cn->next->prev = cn->prev;
  3142. }
  3143. if (cn == journal->j_first) {
  3144. journal->j_first = cn->next;
  3145. }
  3146. if (cn == journal->j_last) {
  3147. journal->j_last = cn->prev;
  3148. }
  3149. if (bh)
  3150. remove_journal_hash(sb, journal->j_hash_table, NULL,
  3151. bh->b_blocknr, 0);
  3152. clear_buffer_journaled(bh); /* don't log this one */
  3153. if (!already_cleaned) {
  3154. clear_buffer_journal_dirty(bh);
  3155. clear_buffer_dirty(bh);
  3156. clear_buffer_journal_test(bh);
  3157. put_bh(bh);
  3158. if (atomic_read(&bh->b_count) < 0) {
  3159. reiserfs_warning(sb, "journal-1752",
  3160. "b_count < 0");
  3161. }
  3162. ret = 1;
  3163. }
  3164. journal->j_len--;
  3165. journal->j_len_alloc--;
  3166. free_cnode(sb, cn);
  3167. return ret;
  3168. }
  3169. /*
  3170. * for any cnode in a journal list, it can only be dirtied of all the
  3171. * transactions that include it are committed to disk.
  3172. * this checks through each transaction, and returns 1 if you are allowed
  3173. * to dirty, and 0 if you aren't
  3174. *
  3175. * it is called by dirty_journal_list, which is called after
  3176. * flush_commit_list has gotten all the log blocks for a given
  3177. * transaction on disk
  3178. *
  3179. */
  3180. static int can_dirty(struct reiserfs_journal_cnode *cn)
  3181. {
  3182. struct super_block *sb = cn->sb;
  3183. b_blocknr_t blocknr = cn->blocknr;
  3184. struct reiserfs_journal_cnode *cur = cn->hprev;
  3185. int can_dirty = 1;
  3186. /*
  3187. * first test hprev. These are all newer than cn, so any node here
  3188. * with the same block number and dev means this node can't be sent
  3189. * to disk right now.
  3190. */
  3191. while (cur && can_dirty) {
  3192. if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
  3193. cur->blocknr == blocknr) {
  3194. can_dirty = 0;
  3195. }
  3196. cur = cur->hprev;
  3197. }
  3198. /*
  3199. * then test hnext. These are all older than cn. As long as they
  3200. * are committed to the log, it is safe to write cn to disk
  3201. */
  3202. cur = cn->hnext;
  3203. while (cur && can_dirty) {
  3204. if (cur->jlist && cur->jlist->j_len > 0 &&
  3205. atomic_read(&cur->jlist->j_commit_left) > 0 && cur->bh &&
  3206. cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
  3207. can_dirty = 0;
  3208. }
  3209. cur = cur->hnext;
  3210. }
  3211. return can_dirty;
  3212. }
  3213. /*
  3214. * syncs the commit blocks, but does not force the real buffers to disk
  3215. * will wait until the current transaction is done/committed before returning
  3216. */
  3217. int journal_end_sync(struct reiserfs_transaction_handle *th)
  3218. {
  3219. struct super_block *sb = th->t_super;
  3220. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3221. BUG_ON(!th->t_trans_id);
  3222. /* you can sync while nested, very, very bad */
  3223. BUG_ON(th->t_refcount > 1);
  3224. if (journal->j_len == 0) {
  3225. reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
  3226. 1);
  3227. journal_mark_dirty(th, SB_BUFFER_WITH_SB(sb));
  3228. }
  3229. return do_journal_end(th, COMMIT_NOW | WAIT);
  3230. }
  3231. /* writeback the pending async commits to disk */
  3232. static void flush_async_commits(struct work_struct *work)
  3233. {
  3234. struct reiserfs_journal *journal =
  3235. container_of(work, struct reiserfs_journal, j_work.work);
  3236. struct super_block *sb = journal->j_work_sb;
  3237. struct reiserfs_journal_list *jl;
  3238. struct list_head *entry;
  3239. reiserfs_write_lock(sb);
  3240. if (!list_empty(&journal->j_journal_list)) {
  3241. /* last entry is the youngest, commit it and you get everything */
  3242. entry = journal->j_journal_list.prev;
  3243. jl = JOURNAL_LIST_ENTRY(entry);
  3244. flush_commit_list(sb, jl, 1);
  3245. }
  3246. reiserfs_write_unlock(sb);
  3247. }
  3248. /*
  3249. * flushes any old transactions to disk
  3250. * ends the current transaction if it is too old
  3251. */
  3252. void reiserfs_flush_old_commits(struct super_block *sb)
  3253. {
  3254. time64_t now;
  3255. struct reiserfs_transaction_handle th;
  3256. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3257. now = ktime_get_seconds();
  3258. /*
  3259. * safety check so we don't flush while we are replaying the log during
  3260. * mount
  3261. */
  3262. if (list_empty(&journal->j_journal_list))
  3263. return;
  3264. /*
  3265. * check the current transaction. If there are no writers, and it is
  3266. * too old, finish it, and force the commit blocks to disk
  3267. */
  3268. if (atomic_read(&journal->j_wcount) <= 0 &&
  3269. journal->j_trans_start_time > 0 &&
  3270. journal->j_len > 0 &&
  3271. (now - journal->j_trans_start_time) > journal->j_max_trans_age) {
  3272. if (!journal_join(&th, sb)) {
  3273. reiserfs_prepare_for_journal(sb,
  3274. SB_BUFFER_WITH_SB(sb),
  3275. 1);
  3276. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(sb));
  3277. /*
  3278. * we're only being called from kreiserfsd, it makes
  3279. * no sense to do an async commit so that kreiserfsd
  3280. * can do it later
  3281. */
  3282. do_journal_end(&th, COMMIT_NOW | WAIT);
  3283. }
  3284. }
  3285. }
  3286. /*
  3287. * returns 0 if do_journal_end should return right away, returns 1 if
  3288. * do_journal_end should finish the commit
  3289. *
  3290. * if the current transaction is too old, but still has writers, this will
  3291. * wait on j_join_wait until all the writers are done. By the time it
  3292. * wakes up, the transaction it was called has already ended, so it just
  3293. * flushes the commit list and returns 0.
  3294. *
  3295. * Won't batch when flush or commit_now is set. Also won't batch when
  3296. * others are waiting on j_join_wait.
  3297. *
  3298. * Note, we can't allow the journal_end to proceed while there are still
  3299. * writers in the log.
  3300. */
  3301. static int check_journal_end(struct reiserfs_transaction_handle *th, int flags)
  3302. {
  3303. time64_t now;
  3304. int flush = flags & FLUSH_ALL;
  3305. int commit_now = flags & COMMIT_NOW;
  3306. int wait_on_commit = flags & WAIT;
  3307. struct reiserfs_journal_list *jl;
  3308. struct super_block *sb = th->t_super;
  3309. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3310. BUG_ON(!th->t_trans_id);
  3311. if (th->t_trans_id != journal->j_trans_id) {
  3312. reiserfs_panic(th->t_super, "journal-1577",
  3313. "handle trans id %ld != current trans id %ld",
  3314. th->t_trans_id, journal->j_trans_id);
  3315. }
  3316. journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged);
  3317. /* <= 0 is allowed. unmounting might not call begin */
  3318. if (atomic_read(&journal->j_wcount) > 0)
  3319. atomic_dec(&journal->j_wcount);
  3320. /*
  3321. * BUG, deal with case where j_len is 0, but people previously
  3322. * freed blocks need to be released will be dealt with by next
  3323. * transaction that actually writes something, but should be taken
  3324. * care of in this trans
  3325. */
  3326. BUG_ON(journal->j_len == 0);
  3327. /*
  3328. * if wcount > 0, and we are called to with flush or commit_now,
  3329. * we wait on j_join_wait. We will wake up when the last writer has
  3330. * finished the transaction, and started it on its way to the disk.
  3331. * Then, we flush the commit or journal list, and just return 0
  3332. * because the rest of journal end was already done for this
  3333. * transaction.
  3334. */
  3335. if (atomic_read(&journal->j_wcount) > 0) {
  3336. if (flush || commit_now) {
  3337. unsigned trans_id;
  3338. jl = journal->j_current_jl;
  3339. trans_id = jl->j_trans_id;
  3340. if (wait_on_commit)
  3341. jl->j_state |= LIST_COMMIT_PENDING;
  3342. atomic_set(&journal->j_jlock, 1);
  3343. if (flush) {
  3344. journal->j_next_full_flush = 1;
  3345. }
  3346. unlock_journal(sb);
  3347. /*
  3348. * sleep while the current transaction is
  3349. * still j_jlocked
  3350. */
  3351. while (journal->j_trans_id == trans_id) {
  3352. if (atomic_read(&journal->j_jlock)) {
  3353. queue_log_writer(sb);
  3354. } else {
  3355. lock_journal(sb);
  3356. if (journal->j_trans_id == trans_id) {
  3357. atomic_set(&journal->j_jlock,
  3358. 1);
  3359. }
  3360. unlock_journal(sb);
  3361. }
  3362. }
  3363. BUG_ON(journal->j_trans_id == trans_id);
  3364. if (commit_now
  3365. && journal_list_still_alive(sb, trans_id)
  3366. && wait_on_commit) {
  3367. flush_commit_list(sb, jl, 1);
  3368. }
  3369. return 0;
  3370. }
  3371. unlock_journal(sb);
  3372. return 0;
  3373. }
  3374. /* deal with old transactions where we are the last writers */
  3375. now = ktime_get_seconds();
  3376. if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
  3377. commit_now = 1;
  3378. journal->j_next_async_flush = 1;
  3379. }
  3380. /* don't batch when someone is waiting on j_join_wait */
  3381. /* don't batch when syncing the commit or flushing the whole trans */
  3382. if (!(journal->j_must_wait > 0) && !(atomic_read(&journal->j_jlock))
  3383. && !flush && !commit_now && (journal->j_len < journal->j_max_batch)
  3384. && journal->j_len_alloc < journal->j_max_batch
  3385. && journal->j_cnode_free > (journal->j_trans_max * 3)) {
  3386. journal->j_bcount++;
  3387. unlock_journal(sb);
  3388. return 0;
  3389. }
  3390. if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(sb)) {
  3391. reiserfs_panic(sb, "journal-003",
  3392. "j_start (%ld) is too high",
  3393. journal->j_start);
  3394. }
  3395. return 1;
  3396. }
  3397. /*
  3398. * Does all the work that makes deleting blocks safe.
  3399. * when deleting a block mark BH_JNew, just remove it from the current
  3400. * transaction, clean it's buffer_head and move on.
  3401. *
  3402. * otherwise:
  3403. * set a bit for the block in the journal bitmap. That will prevent it from
  3404. * being allocated for unformatted nodes before this transaction has finished.
  3405. *
  3406. * mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers.
  3407. * That will prevent any old transactions with this block from trying to flush
  3408. * to the real location. Since we aren't removing the cnode from the
  3409. * journal_list_hash, *the block can't be reallocated yet.
  3410. *
  3411. * Then remove it from the current transaction, decrementing any counters and
  3412. * filing it on the clean list.
  3413. */
  3414. int journal_mark_freed(struct reiserfs_transaction_handle *th,
  3415. struct super_block *sb, b_blocknr_t blocknr)
  3416. {
  3417. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3418. struct reiserfs_journal_cnode *cn = NULL;
  3419. struct buffer_head *bh = NULL;
  3420. struct reiserfs_list_bitmap *jb = NULL;
  3421. int cleaned = 0;
  3422. BUG_ON(!th->t_trans_id);
  3423. cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr);
  3424. if (cn && cn->bh) {
  3425. bh = cn->bh;
  3426. get_bh(bh);
  3427. }
  3428. /* if it is journal new, we just remove it from this transaction */
  3429. if (bh && buffer_journal_new(bh)) {
  3430. clear_buffer_journal_new(bh);
  3431. clear_prepared_bits(bh);
  3432. reiserfs_clean_and_file_buffer(bh);
  3433. cleaned = remove_from_transaction(sb, blocknr, cleaned);
  3434. } else {
  3435. /*
  3436. * set the bit for this block in the journal bitmap
  3437. * for this transaction
  3438. */
  3439. jb = journal->j_current_jl->j_list_bitmap;
  3440. if (!jb) {
  3441. reiserfs_panic(sb, "journal-1702",
  3442. "journal_list_bitmap is NULL");
  3443. }
  3444. set_bit_in_list_bitmap(sb, blocknr, jb);
  3445. /* Note, the entire while loop is not allowed to schedule. */
  3446. if (bh) {
  3447. clear_prepared_bits(bh);
  3448. reiserfs_clean_and_file_buffer(bh);
  3449. }
  3450. cleaned = remove_from_transaction(sb, blocknr, cleaned);
  3451. /*
  3452. * find all older transactions with this block,
  3453. * make sure they don't try to write it out
  3454. */
  3455. cn = get_journal_hash_dev(sb, journal->j_list_hash_table,
  3456. blocknr);
  3457. while (cn) {
  3458. if (sb == cn->sb && blocknr == cn->blocknr) {
  3459. set_bit(BLOCK_FREED, &cn->state);
  3460. if (cn->bh) {
  3461. /*
  3462. * remove_from_transaction will brelse
  3463. * the buffer if it was in the current
  3464. * trans
  3465. */
  3466. if (!cleaned) {
  3467. clear_buffer_journal_dirty(cn->
  3468. bh);
  3469. clear_buffer_dirty(cn->bh);
  3470. clear_buffer_journal_test(cn->
  3471. bh);
  3472. cleaned = 1;
  3473. put_bh(cn->bh);
  3474. if (atomic_read
  3475. (&cn->bh->b_count) < 0) {
  3476. reiserfs_warning(sb,
  3477. "journal-2138",
  3478. "cn->bh->b_count < 0");
  3479. }
  3480. }
  3481. /*
  3482. * since we are clearing the bh,
  3483. * we MUST dec nonzerolen
  3484. */
  3485. if (cn->jlist) {
  3486. atomic_dec(&cn->jlist->
  3487. j_nonzerolen);
  3488. }
  3489. cn->bh = NULL;
  3490. }
  3491. }
  3492. cn = cn->hnext;
  3493. }
  3494. }
  3495. if (bh)
  3496. release_buffer_page(bh); /* get_hash grabs the buffer */
  3497. return 0;
  3498. }
  3499. void reiserfs_update_inode_transaction(struct inode *inode)
  3500. {
  3501. struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb);
  3502. REISERFS_I(inode)->i_jl = journal->j_current_jl;
  3503. REISERFS_I(inode)->i_trans_id = journal->j_trans_id;
  3504. }
  3505. /*
  3506. * returns -1 on error, 0 if no commits/barriers were done and 1
  3507. * if a transaction was actually committed and the barrier was done
  3508. */
  3509. static int __commit_trans_jl(struct inode *inode, unsigned long id,
  3510. struct reiserfs_journal_list *jl)
  3511. {
  3512. struct reiserfs_transaction_handle th;
  3513. struct super_block *sb = inode->i_sb;
  3514. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3515. int ret = 0;
  3516. /*
  3517. * is it from the current transaction,
  3518. * or from an unknown transaction?
  3519. */
  3520. if (id == journal->j_trans_id) {
  3521. jl = journal->j_current_jl;
  3522. /*
  3523. * try to let other writers come in and
  3524. * grow this transaction
  3525. */
  3526. let_transaction_grow(sb, id);
  3527. if (journal->j_trans_id != id) {
  3528. goto flush_commit_only;
  3529. }
  3530. ret = journal_begin(&th, sb, 1);
  3531. if (ret)
  3532. return ret;
  3533. /* someone might have ended this transaction while we joined */
  3534. if (journal->j_trans_id != id) {
  3535. reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
  3536. 1);
  3537. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(sb));
  3538. ret = journal_end(&th);
  3539. goto flush_commit_only;
  3540. }
  3541. ret = journal_end_sync(&th);
  3542. if (!ret)
  3543. ret = 1;
  3544. } else {
  3545. /*
  3546. * this gets tricky, we have to make sure the journal list in
  3547. * the inode still exists. We know the list is still around
  3548. * if we've got a larger transaction id than the oldest list
  3549. */
  3550. flush_commit_only:
  3551. if (journal_list_still_alive(inode->i_sb, id)) {
  3552. /*
  3553. * we only set ret to 1 when we know for sure
  3554. * the barrier hasn't been started yet on the commit
  3555. * block.
  3556. */
  3557. if (atomic_read(&jl->j_commit_left) > 1)
  3558. ret = 1;
  3559. flush_commit_list(sb, jl, 1);
  3560. if (journal->j_errno)
  3561. ret = journal->j_errno;
  3562. }
  3563. }
  3564. /* otherwise the list is gone, and long since committed */
  3565. return ret;
  3566. }
  3567. int reiserfs_commit_for_inode(struct inode *inode)
  3568. {
  3569. unsigned int id = REISERFS_I(inode)->i_trans_id;
  3570. struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
  3571. /*
  3572. * for the whole inode, assume unset id means it was
  3573. * changed in the current transaction. More conservative
  3574. */
  3575. if (!id || !jl) {
  3576. reiserfs_update_inode_transaction(inode);
  3577. id = REISERFS_I(inode)->i_trans_id;
  3578. /* jl will be updated in __commit_trans_jl */
  3579. }
  3580. return __commit_trans_jl(inode, id, jl);
  3581. }
  3582. void reiserfs_restore_prepared_buffer(struct super_block *sb,
  3583. struct buffer_head *bh)
  3584. {
  3585. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3586. PROC_INFO_INC(sb, journal.restore_prepared);
  3587. if (!bh) {
  3588. return;
  3589. }
  3590. if (test_clear_buffer_journal_restore_dirty(bh) &&
  3591. buffer_journal_dirty(bh)) {
  3592. struct reiserfs_journal_cnode *cn;
  3593. reiserfs_write_lock(sb);
  3594. cn = get_journal_hash_dev(sb,
  3595. journal->j_list_hash_table,
  3596. bh->b_blocknr);
  3597. if (cn && can_dirty(cn)) {
  3598. set_buffer_journal_test(bh);
  3599. mark_buffer_dirty(bh);
  3600. }
  3601. reiserfs_write_unlock(sb);
  3602. }
  3603. clear_buffer_journal_prepared(bh);
  3604. }
  3605. extern struct tree_balance *cur_tb;
  3606. /*
  3607. * before we can change a metadata block, we have to make sure it won't
  3608. * be written to disk while we are altering it. So, we must:
  3609. * clean it
  3610. * wait on it.
  3611. */
  3612. int reiserfs_prepare_for_journal(struct super_block *sb,
  3613. struct buffer_head *bh, int wait)
  3614. {
  3615. PROC_INFO_INC(sb, journal.prepare);
  3616. if (!trylock_buffer(bh)) {
  3617. if (!wait)
  3618. return 0;
  3619. lock_buffer(bh);
  3620. }
  3621. set_buffer_journal_prepared(bh);
  3622. if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
  3623. clear_buffer_journal_test(bh);
  3624. set_buffer_journal_restore_dirty(bh);
  3625. }
  3626. unlock_buffer(bh);
  3627. return 1;
  3628. }
  3629. /*
  3630. * long and ugly. If flush, will not return until all commit
  3631. * blocks and all real buffers in the trans are on disk.
  3632. * If no_async, won't return until all commit blocks are on disk.
  3633. *
  3634. * keep reading, there are comments as you go along
  3635. *
  3636. * If the journal is aborted, we just clean up. Things like flushing
  3637. * journal lists, etc just won't happen.
  3638. */
  3639. static int do_journal_end(struct reiserfs_transaction_handle *th, int flags)
  3640. {
  3641. struct super_block *sb = th->t_super;
  3642. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  3643. struct reiserfs_journal_cnode *cn, *next, *jl_cn;
  3644. struct reiserfs_journal_cnode *last_cn = NULL;
  3645. struct reiserfs_journal_desc *desc;
  3646. struct reiserfs_journal_commit *commit;
  3647. struct buffer_head *c_bh; /* commit bh */
  3648. struct buffer_head *d_bh; /* desc bh */
  3649. int cur_write_start = 0; /* start index of current log write */
  3650. int old_start;
  3651. int i;
  3652. int flush;
  3653. int wait_on_commit;
  3654. struct reiserfs_journal_list *jl, *temp_jl;
  3655. struct list_head *entry, *safe;
  3656. unsigned long jindex;
  3657. unsigned int commit_trans_id;
  3658. int trans_half;
  3659. int depth;
  3660. BUG_ON(th->t_refcount > 1);
  3661. BUG_ON(!th->t_trans_id);
  3662. BUG_ON(!th->t_super);
  3663. /*
  3664. * protect flush_older_commits from doing mistakes if the
  3665. * transaction ID counter gets overflowed.
  3666. */
  3667. if (th->t_trans_id == ~0U)
  3668. flags |= FLUSH_ALL | COMMIT_NOW | WAIT;
  3669. flush = flags & FLUSH_ALL;
  3670. wait_on_commit = flags & WAIT;
  3671. current->journal_info = th->t_handle_save;
  3672. reiserfs_check_lock_depth(sb, "journal end");
  3673. if (journal->j_len == 0) {
  3674. reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
  3675. 1);
  3676. journal_mark_dirty(th, SB_BUFFER_WITH_SB(sb));
  3677. }
  3678. lock_journal(sb);
  3679. if (journal->j_next_full_flush) {
  3680. flags |= FLUSH_ALL;
  3681. flush = 1;
  3682. }
  3683. if (journal->j_next_async_flush) {
  3684. flags |= COMMIT_NOW | WAIT;
  3685. wait_on_commit = 1;
  3686. }
  3687. /*
  3688. * check_journal_end locks the journal, and unlocks if it does
  3689. * not return 1 it tells us if we should continue with the
  3690. * journal_end, or just return
  3691. */
  3692. if (!check_journal_end(th, flags)) {
  3693. reiserfs_schedule_old_flush(sb);
  3694. wake_queued_writers(sb);
  3695. reiserfs_async_progress_wait(sb);
  3696. goto out;
  3697. }
  3698. /* check_journal_end might set these, check again */
  3699. if (journal->j_next_full_flush) {
  3700. flush = 1;
  3701. }
  3702. /*
  3703. * j must wait means we have to flush the log blocks, and the
  3704. * real blocks for this transaction
  3705. */
  3706. if (journal->j_must_wait > 0) {
  3707. flush = 1;
  3708. }
  3709. #ifdef REISERFS_PREALLOCATE
  3710. /*
  3711. * quota ops might need to nest, setup the journal_info pointer
  3712. * for them and raise the refcount so that it is > 0.
  3713. */
  3714. current->journal_info = th;
  3715. th->t_refcount++;
  3716. /* it should not involve new blocks into the transaction */
  3717. reiserfs_discard_all_prealloc(th);
  3718. th->t_refcount--;
  3719. current->journal_info = th->t_handle_save;
  3720. #endif
  3721. /* setup description block */
  3722. d_bh =
  3723. journal_getblk(sb,
  3724. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  3725. journal->j_start);
  3726. set_buffer_uptodate(d_bh);
  3727. desc = (struct reiserfs_journal_desc *)(d_bh)->b_data;
  3728. memset(d_bh->b_data, 0, d_bh->b_size);
  3729. memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8);
  3730. set_desc_trans_id(desc, journal->j_trans_id);
  3731. /*
  3732. * setup commit block. Don't write (keep it clean too) this one
  3733. * until after everyone else is written
  3734. */
  3735. c_bh = journal_getblk(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  3736. ((journal->j_start + journal->j_len +
  3737. 1) % SB_ONDISK_JOURNAL_SIZE(sb)));
  3738. commit = (struct reiserfs_journal_commit *)c_bh->b_data;
  3739. memset(c_bh->b_data, 0, c_bh->b_size);
  3740. set_commit_trans_id(commit, journal->j_trans_id);
  3741. set_buffer_uptodate(c_bh);
  3742. /* init this journal list */
  3743. jl = journal->j_current_jl;
  3744. /*
  3745. * we lock the commit before doing anything because
  3746. * we want to make sure nobody tries to run flush_commit_list until
  3747. * the new transaction is fully setup, and we've already flushed the
  3748. * ordered bh list
  3749. */
  3750. reiserfs_mutex_lock_safe(&jl->j_commit_mutex, sb);
  3751. /* save the transaction id in case we need to commit it later */
  3752. commit_trans_id = jl->j_trans_id;
  3753. atomic_set(&jl->j_older_commits_done, 0);
  3754. jl->j_trans_id = journal->j_trans_id;
  3755. jl->j_timestamp = journal->j_trans_start_time;
  3756. jl->j_commit_bh = c_bh;
  3757. jl->j_start = journal->j_start;
  3758. jl->j_len = journal->j_len;
  3759. atomic_set(&jl->j_nonzerolen, journal->j_len);
  3760. atomic_set(&jl->j_commit_left, journal->j_len + 2);
  3761. jl->j_realblock = NULL;
  3762. /*
  3763. * The ENTIRE FOR LOOP MUST not cause schedule to occur.
  3764. * for each real block, add it to the journal list hash,
  3765. * copy into real block index array in the commit or desc block
  3766. */
  3767. trans_half = journal_trans_half(sb->s_blocksize);
  3768. for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) {
  3769. if (buffer_journaled(cn->bh)) {
  3770. jl_cn = get_cnode(sb);
  3771. if (!jl_cn) {
  3772. reiserfs_panic(sb, "journal-1676",
  3773. "get_cnode returned NULL");
  3774. }
  3775. if (i == 0) {
  3776. jl->j_realblock = jl_cn;
  3777. }
  3778. jl_cn->prev = last_cn;
  3779. jl_cn->next = NULL;
  3780. if (last_cn) {
  3781. last_cn->next = jl_cn;
  3782. }
  3783. last_cn = jl_cn;
  3784. /*
  3785. * make sure the block we are trying to log
  3786. * is not a block of journal or reserved area
  3787. */
  3788. if (is_block_in_log_or_reserved_area
  3789. (sb, cn->bh->b_blocknr)) {
  3790. reiserfs_panic(sb, "journal-2332",
  3791. "Trying to log block %lu, "
  3792. "which is a log block",
  3793. cn->bh->b_blocknr);
  3794. }
  3795. jl_cn->blocknr = cn->bh->b_blocknr;
  3796. jl_cn->state = 0;
  3797. jl_cn->sb = sb;
  3798. jl_cn->bh = cn->bh;
  3799. jl_cn->jlist = jl;
  3800. insert_journal_hash(journal->j_list_hash_table, jl_cn);
  3801. if (i < trans_half) {
  3802. desc->j_realblock[i] =
  3803. cpu_to_le32(cn->bh->b_blocknr);
  3804. } else {
  3805. commit->j_realblock[i - trans_half] =
  3806. cpu_to_le32(cn->bh->b_blocknr);
  3807. }
  3808. } else {
  3809. i--;
  3810. }
  3811. }
  3812. set_desc_trans_len(desc, journal->j_len);
  3813. set_desc_mount_id(desc, journal->j_mount_id);
  3814. set_desc_trans_id(desc, journal->j_trans_id);
  3815. set_commit_trans_len(commit, journal->j_len);
  3816. /*
  3817. * special check in case all buffers in the journal
  3818. * were marked for not logging
  3819. */
  3820. BUG_ON(journal->j_len == 0);
  3821. /*
  3822. * we're about to dirty all the log blocks, mark the description block
  3823. * dirty now too. Don't mark the commit block dirty until all the
  3824. * others are on disk
  3825. */
  3826. mark_buffer_dirty(d_bh);
  3827. /*
  3828. * first data block is j_start + 1, so add one to
  3829. * cur_write_start wherever you use it
  3830. */
  3831. cur_write_start = journal->j_start;
  3832. cn = journal->j_first;
  3833. jindex = 1; /* start at one so we don't get the desc again */
  3834. while (cn) {
  3835. clear_buffer_journal_new(cn->bh);
  3836. /* copy all the real blocks into log area. dirty log blocks */
  3837. if (buffer_journaled(cn->bh)) {
  3838. struct buffer_head *tmp_bh;
  3839. char *addr;
  3840. struct page *page;
  3841. tmp_bh =
  3842. journal_getblk(sb,
  3843. SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
  3844. ((cur_write_start +
  3845. jindex) %
  3846. SB_ONDISK_JOURNAL_SIZE(sb)));
  3847. set_buffer_uptodate(tmp_bh);
  3848. page = cn->bh->b_page;
  3849. addr = kmap(page);
  3850. memcpy(tmp_bh->b_data,
  3851. addr + offset_in_page(cn->bh->b_data),
  3852. cn->bh->b_size);
  3853. kunmap(page);
  3854. mark_buffer_dirty(tmp_bh);
  3855. jindex++;
  3856. set_buffer_journal_dirty(cn->bh);
  3857. clear_buffer_journaled(cn->bh);
  3858. } else {
  3859. /*
  3860. * JDirty cleared sometime during transaction.
  3861. * don't log this one
  3862. */
  3863. reiserfs_warning(sb, "journal-2048",
  3864. "BAD, buffer in journal hash, "
  3865. "but not JDirty!");
  3866. brelse(cn->bh);
  3867. }
  3868. next = cn->next;
  3869. free_cnode(sb, cn);
  3870. cn = next;
  3871. reiserfs_cond_resched(sb);
  3872. }
  3873. /*
  3874. * we are done with both the c_bh and d_bh, but
  3875. * c_bh must be written after all other commit blocks,
  3876. * so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
  3877. */
  3878. journal->j_current_jl = alloc_journal_list(sb);
  3879. /* now it is safe to insert this transaction on the main list */
  3880. list_add_tail(&jl->j_list, &journal->j_journal_list);
  3881. list_add_tail(&jl->j_working_list, &journal->j_working_list);
  3882. journal->j_num_work_lists++;
  3883. /* reset journal values for the next transaction */
  3884. old_start = journal->j_start;
  3885. journal->j_start =
  3886. (journal->j_start + journal->j_len +
  3887. 2) % SB_ONDISK_JOURNAL_SIZE(sb);
  3888. atomic_set(&journal->j_wcount, 0);
  3889. journal->j_bcount = 0;
  3890. journal->j_last = NULL;
  3891. journal->j_first = NULL;
  3892. journal->j_len = 0;
  3893. journal->j_trans_start_time = 0;
  3894. /* check for trans_id overflow */
  3895. if (++journal->j_trans_id == 0)
  3896. journal->j_trans_id = 10;
  3897. journal->j_current_jl->j_trans_id = journal->j_trans_id;
  3898. journal->j_must_wait = 0;
  3899. journal->j_len_alloc = 0;
  3900. journal->j_next_full_flush = 0;
  3901. journal->j_next_async_flush = 0;
  3902. init_journal_hash(sb);
  3903. /*
  3904. * make sure reiserfs_add_jh sees the new current_jl before we
  3905. * write out the tails
  3906. */
  3907. smp_mb();
  3908. /*
  3909. * tail conversion targets have to hit the disk before we end the
  3910. * transaction. Otherwise a later transaction might repack the tail
  3911. * before this transaction commits, leaving the data block unflushed
  3912. * and clean, if we crash before the later transaction commits, the
  3913. * data block is lost.
  3914. */
  3915. if (!list_empty(&jl->j_tail_bh_list)) {
  3916. depth = reiserfs_write_unlock_nested(sb);
  3917. write_ordered_buffers(&journal->j_dirty_buffers_lock,
  3918. journal, jl, &jl->j_tail_bh_list);
  3919. reiserfs_write_lock_nested(sb, depth);
  3920. }
  3921. BUG_ON(!list_empty(&jl->j_tail_bh_list));
  3922. mutex_unlock(&jl->j_commit_mutex);
  3923. /*
  3924. * honor the flush wishes from the caller, simple commits can
  3925. * be done outside the journal lock, they are done below
  3926. *
  3927. * if we don't flush the commit list right now, we put it into
  3928. * the work queue so the people waiting on the async progress work
  3929. * queue don't wait for this proc to flush journal lists and such.
  3930. */
  3931. if (flush) {
  3932. flush_commit_list(sb, jl, 1);
  3933. flush_journal_list(sb, jl, 1);
  3934. } else if (!(jl->j_state & LIST_COMMIT_PENDING)) {
  3935. /*
  3936. * Avoid queueing work when sb is being shut down. Transaction
  3937. * will be flushed on journal shutdown.
  3938. */
  3939. if (sb->s_flags & SB_ACTIVE)
  3940. queue_delayed_work(REISERFS_SB(sb)->commit_wq,
  3941. &journal->j_work, HZ / 10);
  3942. }
  3943. /*
  3944. * if the next transaction has any chance of wrapping, flush
  3945. * transactions that might get overwritten. If any journal lists
  3946. * are very old flush them as well.
  3947. */
  3948. first_jl:
  3949. list_for_each_safe(entry, safe, &journal->j_journal_list) {
  3950. temp_jl = JOURNAL_LIST_ENTRY(entry);
  3951. if (journal->j_start <= temp_jl->j_start) {
  3952. if ((journal->j_start + journal->j_trans_max + 1) >=
  3953. temp_jl->j_start) {
  3954. flush_used_journal_lists(sb, temp_jl);
  3955. goto first_jl;
  3956. } else if ((journal->j_start +
  3957. journal->j_trans_max + 1) <
  3958. SB_ONDISK_JOURNAL_SIZE(sb)) {
  3959. /*
  3960. * if we don't cross into the next
  3961. * transaction and we don't wrap, there is
  3962. * no way we can overlap any later transactions
  3963. * break now
  3964. */
  3965. break;
  3966. }
  3967. } else if ((journal->j_start +
  3968. journal->j_trans_max + 1) >
  3969. SB_ONDISK_JOURNAL_SIZE(sb)) {
  3970. if (((journal->j_start + journal->j_trans_max + 1) %
  3971. SB_ONDISK_JOURNAL_SIZE(sb)) >=
  3972. temp_jl->j_start) {
  3973. flush_used_journal_lists(sb, temp_jl);
  3974. goto first_jl;
  3975. } else {
  3976. /*
  3977. * we don't overlap anything from out start
  3978. * to the end of the log, and our wrapped
  3979. * portion doesn't overlap anything at
  3980. * the start of the log. We can break
  3981. */
  3982. break;
  3983. }
  3984. }
  3985. }
  3986. journal->j_current_jl->j_list_bitmap =
  3987. get_list_bitmap(sb, journal->j_current_jl);
  3988. if (!(journal->j_current_jl->j_list_bitmap)) {
  3989. reiserfs_panic(sb, "journal-1996",
  3990. "could not get a list bitmap");
  3991. }
  3992. atomic_set(&journal->j_jlock, 0);
  3993. unlock_journal(sb);
  3994. /* wake up any body waiting to join. */
  3995. clear_bit(J_WRITERS_QUEUED, &journal->j_state);
  3996. wake_up(&journal->j_join_wait);
  3997. if (!flush && wait_on_commit &&
  3998. journal_list_still_alive(sb, commit_trans_id)) {
  3999. flush_commit_list(sb, jl, 1);
  4000. }
  4001. out:
  4002. reiserfs_check_lock_depth(sb, "journal end2");
  4003. memset(th, 0, sizeof(*th));
  4004. /*
  4005. * Re-set th->t_super, so we can properly keep track of how many
  4006. * persistent transactions there are. We need to do this so if this
  4007. * call is part of a failed restart_transaction, we can free it later
  4008. */
  4009. th->t_super = sb;
  4010. return journal->j_errno;
  4011. }
  4012. /* Send the file system read only and refuse new transactions */
  4013. void reiserfs_abort_journal(struct super_block *sb, int errno)
  4014. {
  4015. struct reiserfs_journal *journal = SB_JOURNAL(sb);
  4016. if (test_bit(J_ABORTED, &journal->j_state))
  4017. return;
  4018. if (!journal->j_errno)
  4019. journal->j_errno = errno;
  4020. sb->s_flags |= SB_RDONLY;
  4021. set_bit(J_ABORTED, &journal->j_state);
  4022. #ifdef CONFIG_REISERFS_CHECK
  4023. dump_stack();
  4024. #endif
  4025. }