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