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