journal.c 122 KB

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