journal.c 120 KB

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