journal.c 120 KB

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