journal.c 124 KB

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