journal.c 121 KB

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