journal.c 121 KB

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