journal.c 120 KB

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