journal.c 121 KB

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