journal.c 124 KB

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