journal.c 123 KB

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