journal.c 124 KB

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