journal.c 124 KB

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