tree-log.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/list_sort.h>
  22. #include "tree-log.h"
  23. #include "disk-io.h"
  24. #include "locking.h"
  25. #include "print-tree.h"
  26. #include "backref.h"
  27. #include "hash.h"
  28. /* magic values for the inode_only field in btrfs_log_inode:
  29. *
  30. * LOG_INODE_ALL means to log everything
  31. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  32. * during log replay
  33. */
  34. #define LOG_INODE_ALL 0
  35. #define LOG_INODE_EXISTS 1
  36. /*
  37. * directory trouble cases
  38. *
  39. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  40. * log, we must force a full commit before doing an fsync of the directory
  41. * where the unlink was done.
  42. * ---> record transid of last unlink/rename per directory
  43. *
  44. * mkdir foo/some_dir
  45. * normal commit
  46. * rename foo/some_dir foo2/some_dir
  47. * mkdir foo/some_dir
  48. * fsync foo/some_dir/some_file
  49. *
  50. * The fsync above will unlink the original some_dir without recording
  51. * it in its new location (foo2). After a crash, some_dir will be gone
  52. * unless the fsync of some_file forces a full commit
  53. *
  54. * 2) we must log any new names for any file or dir that is in the fsync
  55. * log. ---> check inode while renaming/linking.
  56. *
  57. * 2a) we must log any new names for any file or dir during rename
  58. * when the directory they are being removed from was logged.
  59. * ---> check inode and old parent dir during rename
  60. *
  61. * 2a is actually the more important variant. With the extra logging
  62. * a crash might unlink the old name without recreating the new one
  63. *
  64. * 3) after a crash, we must go through any directories with a link count
  65. * of zero and redo the rm -rf
  66. *
  67. * mkdir f1/foo
  68. * normal commit
  69. * rm -rf f1/foo
  70. * fsync(f1)
  71. *
  72. * The directory f1 was fully removed from the FS, but fsync was never
  73. * called on f1, only its parent dir. After a crash the rm -rf must
  74. * be replayed. This must be able to recurse down the entire
  75. * directory tree. The inode link count fixup code takes care of the
  76. * ugly details.
  77. */
  78. /*
  79. * stages for the tree walking. The first
  80. * stage (0) is to only pin down the blocks we find
  81. * the second stage (1) is to make sure that all the inodes
  82. * we find in the log are created in the subvolume.
  83. *
  84. * The last stage is to deal with directories and links and extents
  85. * and all the other fun semantics
  86. */
  87. #define LOG_WALK_PIN_ONLY 0
  88. #define LOG_WALK_REPLAY_INODES 1
  89. #define LOG_WALK_REPLAY_DIR_INDEX 2
  90. #define LOG_WALK_REPLAY_ALL 3
  91. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  92. struct btrfs_root *root, struct inode *inode,
  93. int inode_only,
  94. const loff_t start,
  95. const loff_t end);
  96. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  97. struct btrfs_root *root,
  98. struct btrfs_path *path, u64 objectid);
  99. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root,
  101. struct btrfs_root *log,
  102. struct btrfs_path *path,
  103. u64 dirid, int del_all);
  104. /*
  105. * tree logging is a special write ahead log used to make sure that
  106. * fsyncs and O_SYNCs can happen without doing full tree commits.
  107. *
  108. * Full tree commits are expensive because they require commonly
  109. * modified blocks to be recowed, creating many dirty pages in the
  110. * extent tree an 4x-6x higher write load than ext3.
  111. *
  112. * Instead of doing a tree commit on every fsync, we use the
  113. * key ranges and transaction ids to find items for a given file or directory
  114. * that have changed in this transaction. Those items are copied into
  115. * a special tree (one per subvolume root), that tree is written to disk
  116. * and then the fsync is considered complete.
  117. *
  118. * After a crash, items are copied out of the log-tree back into the
  119. * subvolume tree. Any file data extents found are recorded in the extent
  120. * allocation tree, and the log-tree freed.
  121. *
  122. * The log tree is read three times, once to pin down all the extents it is
  123. * using in ram and once, once to create all the inodes logged in the tree
  124. * and once to do all the other items.
  125. */
  126. /*
  127. * start a sub transaction and setup the log tree
  128. * this increments the log tree writer count to make the people
  129. * syncing the tree wait for us to finish
  130. */
  131. static int start_log_trans(struct btrfs_trans_handle *trans,
  132. struct btrfs_root *root,
  133. struct btrfs_log_ctx *ctx)
  134. {
  135. int index;
  136. int ret;
  137. mutex_lock(&root->log_mutex);
  138. if (root->log_root) {
  139. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  140. ret = -EAGAIN;
  141. goto out;
  142. }
  143. if (!root->log_start_pid) {
  144. root->log_start_pid = current->pid;
  145. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  146. } else if (root->log_start_pid != current->pid) {
  147. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  148. }
  149. atomic_inc(&root->log_batch);
  150. atomic_inc(&root->log_writers);
  151. if (ctx) {
  152. index = root->log_transid % 2;
  153. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  154. ctx->log_transid = root->log_transid;
  155. }
  156. mutex_unlock(&root->log_mutex);
  157. return 0;
  158. }
  159. ret = 0;
  160. mutex_lock(&root->fs_info->tree_log_mutex);
  161. if (!root->fs_info->log_root_tree)
  162. ret = btrfs_init_log_root_tree(trans, root->fs_info);
  163. mutex_unlock(&root->fs_info->tree_log_mutex);
  164. if (ret)
  165. goto out;
  166. if (!root->log_root) {
  167. ret = btrfs_add_log_tree(trans, root);
  168. if (ret)
  169. goto out;
  170. }
  171. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  172. root->log_start_pid = current->pid;
  173. atomic_inc(&root->log_batch);
  174. atomic_inc(&root->log_writers);
  175. if (ctx) {
  176. index = root->log_transid % 2;
  177. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  178. ctx->log_transid = root->log_transid;
  179. }
  180. out:
  181. mutex_unlock(&root->log_mutex);
  182. return ret;
  183. }
  184. /*
  185. * returns 0 if there was a log transaction running and we were able
  186. * to join, or returns -ENOENT if there were not transactions
  187. * in progress
  188. */
  189. static int join_running_log_trans(struct btrfs_root *root)
  190. {
  191. int ret = -ENOENT;
  192. smp_mb();
  193. if (!root->log_root)
  194. return -ENOENT;
  195. mutex_lock(&root->log_mutex);
  196. if (root->log_root) {
  197. ret = 0;
  198. atomic_inc(&root->log_writers);
  199. }
  200. mutex_unlock(&root->log_mutex);
  201. return ret;
  202. }
  203. /*
  204. * This either makes the current running log transaction wait
  205. * until you call btrfs_end_log_trans() or it makes any future
  206. * log transactions wait until you call btrfs_end_log_trans()
  207. */
  208. int btrfs_pin_log_trans(struct btrfs_root *root)
  209. {
  210. int ret = -ENOENT;
  211. mutex_lock(&root->log_mutex);
  212. atomic_inc(&root->log_writers);
  213. mutex_unlock(&root->log_mutex);
  214. return ret;
  215. }
  216. /*
  217. * indicate we're done making changes to the log tree
  218. * and wake up anyone waiting to do a sync
  219. */
  220. void btrfs_end_log_trans(struct btrfs_root *root)
  221. {
  222. if (atomic_dec_and_test(&root->log_writers)) {
  223. smp_mb();
  224. if (waitqueue_active(&root->log_writer_wait))
  225. wake_up(&root->log_writer_wait);
  226. }
  227. }
  228. /*
  229. * the walk control struct is used to pass state down the chain when
  230. * processing the log tree. The stage field tells us which part
  231. * of the log tree processing we are currently doing. The others
  232. * are state fields used for that specific part
  233. */
  234. struct walk_control {
  235. /* should we free the extent on disk when done? This is used
  236. * at transaction commit time while freeing a log tree
  237. */
  238. int free;
  239. /* should we write out the extent buffer? This is used
  240. * while flushing the log tree to disk during a sync
  241. */
  242. int write;
  243. /* should we wait for the extent buffer io to finish? Also used
  244. * while flushing the log tree to disk for a sync
  245. */
  246. int wait;
  247. /* pin only walk, we record which extents on disk belong to the
  248. * log trees
  249. */
  250. int pin;
  251. /* what stage of the replay code we're currently in */
  252. int stage;
  253. /* the root we are currently replaying */
  254. struct btrfs_root *replay_dest;
  255. /* the trans handle for the current replay */
  256. struct btrfs_trans_handle *trans;
  257. /* the function that gets used to process blocks we find in the
  258. * tree. Note the extent_buffer might not be up to date when it is
  259. * passed in, and it must be checked or read if you need the data
  260. * inside it
  261. */
  262. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  263. struct walk_control *wc, u64 gen);
  264. };
  265. /*
  266. * process_func used to pin down extents, write them or wait on them
  267. */
  268. static int process_one_buffer(struct btrfs_root *log,
  269. struct extent_buffer *eb,
  270. struct walk_control *wc, u64 gen)
  271. {
  272. int ret = 0;
  273. /*
  274. * If this fs is mixed then we need to be able to process the leaves to
  275. * pin down any logged extents, so we have to read the block.
  276. */
  277. if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
  278. ret = btrfs_read_buffer(eb, gen);
  279. if (ret)
  280. return ret;
  281. }
  282. if (wc->pin)
  283. ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
  284. eb->start, eb->len);
  285. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  286. if (wc->pin && btrfs_header_level(eb) == 0)
  287. ret = btrfs_exclude_logged_extents(log, eb);
  288. if (wc->write)
  289. btrfs_write_tree_block(eb);
  290. if (wc->wait)
  291. btrfs_wait_tree_block_writeback(eb);
  292. }
  293. return ret;
  294. }
  295. /*
  296. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  297. * to the src data we are copying out.
  298. *
  299. * root is the tree we are copying into, and path is a scratch
  300. * path for use in this function (it should be released on entry and
  301. * will be released on exit).
  302. *
  303. * If the key is already in the destination tree the existing item is
  304. * overwritten. If the existing item isn't big enough, it is extended.
  305. * If it is too large, it is truncated.
  306. *
  307. * If the key isn't in the destination yet, a new item is inserted.
  308. */
  309. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  310. struct btrfs_root *root,
  311. struct btrfs_path *path,
  312. struct extent_buffer *eb, int slot,
  313. struct btrfs_key *key)
  314. {
  315. int ret;
  316. u32 item_size;
  317. u64 saved_i_size = 0;
  318. int save_old_i_size = 0;
  319. unsigned long src_ptr;
  320. unsigned long dst_ptr;
  321. int overwrite_root = 0;
  322. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  323. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  324. overwrite_root = 1;
  325. item_size = btrfs_item_size_nr(eb, slot);
  326. src_ptr = btrfs_item_ptr_offset(eb, slot);
  327. /* look for the key in the destination tree */
  328. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  329. if (ret < 0)
  330. return ret;
  331. if (ret == 0) {
  332. char *src_copy;
  333. char *dst_copy;
  334. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  335. path->slots[0]);
  336. if (dst_size != item_size)
  337. goto insert;
  338. if (item_size == 0) {
  339. btrfs_release_path(path);
  340. return 0;
  341. }
  342. dst_copy = kmalloc(item_size, GFP_NOFS);
  343. src_copy = kmalloc(item_size, GFP_NOFS);
  344. if (!dst_copy || !src_copy) {
  345. btrfs_release_path(path);
  346. kfree(dst_copy);
  347. kfree(src_copy);
  348. return -ENOMEM;
  349. }
  350. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  351. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  352. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  353. item_size);
  354. ret = memcmp(dst_copy, src_copy, item_size);
  355. kfree(dst_copy);
  356. kfree(src_copy);
  357. /*
  358. * they have the same contents, just return, this saves
  359. * us from cowing blocks in the destination tree and doing
  360. * extra writes that may not have been done by a previous
  361. * sync
  362. */
  363. if (ret == 0) {
  364. btrfs_release_path(path);
  365. return 0;
  366. }
  367. /*
  368. * We need to load the old nbytes into the inode so when we
  369. * replay the extents we've logged we get the right nbytes.
  370. */
  371. if (inode_item) {
  372. struct btrfs_inode_item *item;
  373. u64 nbytes;
  374. u32 mode;
  375. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  376. struct btrfs_inode_item);
  377. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  378. item = btrfs_item_ptr(eb, slot,
  379. struct btrfs_inode_item);
  380. btrfs_set_inode_nbytes(eb, item, nbytes);
  381. /*
  382. * If this is a directory we need to reset the i_size to
  383. * 0 so that we can set it up properly when replaying
  384. * the rest of the items in this log.
  385. */
  386. mode = btrfs_inode_mode(eb, item);
  387. if (S_ISDIR(mode))
  388. btrfs_set_inode_size(eb, item, 0);
  389. }
  390. } else if (inode_item) {
  391. struct btrfs_inode_item *item;
  392. u32 mode;
  393. /*
  394. * New inode, set nbytes to 0 so that the nbytes comes out
  395. * properly when we replay the extents.
  396. */
  397. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  398. btrfs_set_inode_nbytes(eb, item, 0);
  399. /*
  400. * If this is a directory we need to reset the i_size to 0 so
  401. * that we can set it up properly when replaying the rest of
  402. * the items in this log.
  403. */
  404. mode = btrfs_inode_mode(eb, item);
  405. if (S_ISDIR(mode))
  406. btrfs_set_inode_size(eb, item, 0);
  407. }
  408. insert:
  409. btrfs_release_path(path);
  410. /* try to insert the key into the destination tree */
  411. ret = btrfs_insert_empty_item(trans, root, path,
  412. key, item_size);
  413. /* make sure any existing item is the correct size */
  414. if (ret == -EEXIST) {
  415. u32 found_size;
  416. found_size = btrfs_item_size_nr(path->nodes[0],
  417. path->slots[0]);
  418. if (found_size > item_size)
  419. btrfs_truncate_item(root, path, item_size, 1);
  420. else if (found_size < item_size)
  421. btrfs_extend_item(root, path,
  422. item_size - found_size);
  423. } else if (ret) {
  424. return ret;
  425. }
  426. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  427. path->slots[0]);
  428. /* don't overwrite an existing inode if the generation number
  429. * was logged as zero. This is done when the tree logging code
  430. * is just logging an inode to make sure it exists after recovery.
  431. *
  432. * Also, don't overwrite i_size on directories during replay.
  433. * log replay inserts and removes directory items based on the
  434. * state of the tree found in the subvolume, and i_size is modified
  435. * as it goes
  436. */
  437. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  438. struct btrfs_inode_item *src_item;
  439. struct btrfs_inode_item *dst_item;
  440. src_item = (struct btrfs_inode_item *)src_ptr;
  441. dst_item = (struct btrfs_inode_item *)dst_ptr;
  442. if (btrfs_inode_generation(eb, src_item) == 0)
  443. goto no_copy;
  444. if (overwrite_root &&
  445. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  446. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  447. save_old_i_size = 1;
  448. saved_i_size = btrfs_inode_size(path->nodes[0],
  449. dst_item);
  450. }
  451. }
  452. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  453. src_ptr, item_size);
  454. if (save_old_i_size) {
  455. struct btrfs_inode_item *dst_item;
  456. dst_item = (struct btrfs_inode_item *)dst_ptr;
  457. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  458. }
  459. /* make sure the generation is filled in */
  460. if (key->type == BTRFS_INODE_ITEM_KEY) {
  461. struct btrfs_inode_item *dst_item;
  462. dst_item = (struct btrfs_inode_item *)dst_ptr;
  463. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  464. btrfs_set_inode_generation(path->nodes[0], dst_item,
  465. trans->transid);
  466. }
  467. }
  468. no_copy:
  469. btrfs_mark_buffer_dirty(path->nodes[0]);
  470. btrfs_release_path(path);
  471. return 0;
  472. }
  473. /*
  474. * simple helper to read an inode off the disk from a given root
  475. * This can only be called for subvolume roots and not for the log
  476. */
  477. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  478. u64 objectid)
  479. {
  480. struct btrfs_key key;
  481. struct inode *inode;
  482. key.objectid = objectid;
  483. key.type = BTRFS_INODE_ITEM_KEY;
  484. key.offset = 0;
  485. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  486. if (IS_ERR(inode)) {
  487. inode = NULL;
  488. } else if (is_bad_inode(inode)) {
  489. iput(inode);
  490. inode = NULL;
  491. }
  492. return inode;
  493. }
  494. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  495. * subvolume 'root'. path is released on entry and should be released
  496. * on exit.
  497. *
  498. * extents in the log tree have not been allocated out of the extent
  499. * tree yet. So, this completes the allocation, taking a reference
  500. * as required if the extent already exists or creating a new extent
  501. * if it isn't in the extent allocation tree yet.
  502. *
  503. * The extent is inserted into the file, dropping any existing extents
  504. * from the file that overlap the new one.
  505. */
  506. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  507. struct btrfs_root *root,
  508. struct btrfs_path *path,
  509. struct extent_buffer *eb, int slot,
  510. struct btrfs_key *key)
  511. {
  512. int found_type;
  513. u64 extent_end;
  514. u64 start = key->offset;
  515. u64 nbytes = 0;
  516. struct btrfs_file_extent_item *item;
  517. struct inode *inode = NULL;
  518. unsigned long size;
  519. int ret = 0;
  520. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  521. found_type = btrfs_file_extent_type(eb, item);
  522. if (found_type == BTRFS_FILE_EXTENT_REG ||
  523. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  524. nbytes = btrfs_file_extent_num_bytes(eb, item);
  525. extent_end = start + nbytes;
  526. /*
  527. * We don't add to the inodes nbytes if we are prealloc or a
  528. * hole.
  529. */
  530. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  531. nbytes = 0;
  532. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  533. size = btrfs_file_extent_inline_len(eb, slot, item);
  534. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  535. extent_end = ALIGN(start + size, root->sectorsize);
  536. } else {
  537. ret = 0;
  538. goto out;
  539. }
  540. inode = read_one_inode(root, key->objectid);
  541. if (!inode) {
  542. ret = -EIO;
  543. goto out;
  544. }
  545. /*
  546. * first check to see if we already have this extent in the
  547. * file. This must be done before the btrfs_drop_extents run
  548. * so we don't try to drop this extent.
  549. */
  550. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  551. start, 0);
  552. if (ret == 0 &&
  553. (found_type == BTRFS_FILE_EXTENT_REG ||
  554. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  555. struct btrfs_file_extent_item cmp1;
  556. struct btrfs_file_extent_item cmp2;
  557. struct btrfs_file_extent_item *existing;
  558. struct extent_buffer *leaf;
  559. leaf = path->nodes[0];
  560. existing = btrfs_item_ptr(leaf, path->slots[0],
  561. struct btrfs_file_extent_item);
  562. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  563. sizeof(cmp1));
  564. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  565. sizeof(cmp2));
  566. /*
  567. * we already have a pointer to this exact extent,
  568. * we don't have to do anything
  569. */
  570. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  571. btrfs_release_path(path);
  572. goto out;
  573. }
  574. }
  575. btrfs_release_path(path);
  576. /* drop any overlapping extents */
  577. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  578. if (ret)
  579. goto out;
  580. if (found_type == BTRFS_FILE_EXTENT_REG ||
  581. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  582. u64 offset;
  583. unsigned long dest_offset;
  584. struct btrfs_key ins;
  585. ret = btrfs_insert_empty_item(trans, root, path, key,
  586. sizeof(*item));
  587. if (ret)
  588. goto out;
  589. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  590. path->slots[0]);
  591. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  592. (unsigned long)item, sizeof(*item));
  593. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  594. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  595. ins.type = BTRFS_EXTENT_ITEM_KEY;
  596. offset = key->offset - btrfs_file_extent_offset(eb, item);
  597. if (ins.objectid > 0) {
  598. u64 csum_start;
  599. u64 csum_end;
  600. LIST_HEAD(ordered_sums);
  601. /*
  602. * is this extent already allocated in the extent
  603. * allocation tree? If so, just add a reference
  604. */
  605. ret = btrfs_lookup_extent(root, ins.objectid,
  606. ins.offset);
  607. if (ret == 0) {
  608. ret = btrfs_inc_extent_ref(trans, root,
  609. ins.objectid, ins.offset,
  610. 0, root->root_key.objectid,
  611. key->objectid, offset, 0);
  612. if (ret)
  613. goto out;
  614. } else {
  615. /*
  616. * insert the extent pointer in the extent
  617. * allocation tree
  618. */
  619. ret = btrfs_alloc_logged_file_extent(trans,
  620. root, root->root_key.objectid,
  621. key->objectid, offset, &ins);
  622. if (ret)
  623. goto out;
  624. }
  625. btrfs_release_path(path);
  626. if (btrfs_file_extent_compression(eb, item)) {
  627. csum_start = ins.objectid;
  628. csum_end = csum_start + ins.offset;
  629. } else {
  630. csum_start = ins.objectid +
  631. btrfs_file_extent_offset(eb, item);
  632. csum_end = csum_start +
  633. btrfs_file_extent_num_bytes(eb, item);
  634. }
  635. ret = btrfs_lookup_csums_range(root->log_root,
  636. csum_start, csum_end - 1,
  637. &ordered_sums, 0);
  638. if (ret)
  639. goto out;
  640. while (!list_empty(&ordered_sums)) {
  641. struct btrfs_ordered_sum *sums;
  642. sums = list_entry(ordered_sums.next,
  643. struct btrfs_ordered_sum,
  644. list);
  645. if (!ret)
  646. ret = btrfs_csum_file_blocks(trans,
  647. root->fs_info->csum_root,
  648. sums);
  649. list_del(&sums->list);
  650. kfree(sums);
  651. }
  652. if (ret)
  653. goto out;
  654. } else {
  655. btrfs_release_path(path);
  656. }
  657. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  658. /* inline extents are easy, we just overwrite them */
  659. ret = overwrite_item(trans, root, path, eb, slot, key);
  660. if (ret)
  661. goto out;
  662. }
  663. inode_add_bytes(inode, nbytes);
  664. ret = btrfs_update_inode(trans, root, inode);
  665. out:
  666. if (inode)
  667. iput(inode);
  668. return ret;
  669. }
  670. /*
  671. * when cleaning up conflicts between the directory names in the
  672. * subvolume, directory names in the log and directory names in the
  673. * inode back references, we may have to unlink inodes from directories.
  674. *
  675. * This is a helper function to do the unlink of a specific directory
  676. * item
  677. */
  678. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  679. struct btrfs_root *root,
  680. struct btrfs_path *path,
  681. struct inode *dir,
  682. struct btrfs_dir_item *di)
  683. {
  684. struct inode *inode;
  685. char *name;
  686. int name_len;
  687. struct extent_buffer *leaf;
  688. struct btrfs_key location;
  689. int ret;
  690. leaf = path->nodes[0];
  691. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  692. name_len = btrfs_dir_name_len(leaf, di);
  693. name = kmalloc(name_len, GFP_NOFS);
  694. if (!name)
  695. return -ENOMEM;
  696. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  697. btrfs_release_path(path);
  698. inode = read_one_inode(root, location.objectid);
  699. if (!inode) {
  700. ret = -EIO;
  701. goto out;
  702. }
  703. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  704. if (ret)
  705. goto out;
  706. ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  707. if (ret)
  708. goto out;
  709. else
  710. ret = btrfs_run_delayed_items(trans, root);
  711. out:
  712. kfree(name);
  713. iput(inode);
  714. return ret;
  715. }
  716. /*
  717. * helper function to see if a given name and sequence number found
  718. * in an inode back reference are already in a directory and correctly
  719. * point to this inode
  720. */
  721. static noinline int inode_in_dir(struct btrfs_root *root,
  722. struct btrfs_path *path,
  723. u64 dirid, u64 objectid, u64 index,
  724. const char *name, int name_len)
  725. {
  726. struct btrfs_dir_item *di;
  727. struct btrfs_key location;
  728. int match = 0;
  729. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  730. index, name, name_len, 0);
  731. if (di && !IS_ERR(di)) {
  732. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  733. if (location.objectid != objectid)
  734. goto out;
  735. } else
  736. goto out;
  737. btrfs_release_path(path);
  738. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  739. if (di && !IS_ERR(di)) {
  740. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  741. if (location.objectid != objectid)
  742. goto out;
  743. } else
  744. goto out;
  745. match = 1;
  746. out:
  747. btrfs_release_path(path);
  748. return match;
  749. }
  750. /*
  751. * helper function to check a log tree for a named back reference in
  752. * an inode. This is used to decide if a back reference that is
  753. * found in the subvolume conflicts with what we find in the log.
  754. *
  755. * inode backreferences may have multiple refs in a single item,
  756. * during replay we process one reference at a time, and we don't
  757. * want to delete valid links to a file from the subvolume if that
  758. * link is also in the log.
  759. */
  760. static noinline int backref_in_log(struct btrfs_root *log,
  761. struct btrfs_key *key,
  762. u64 ref_objectid,
  763. char *name, int namelen)
  764. {
  765. struct btrfs_path *path;
  766. struct btrfs_inode_ref *ref;
  767. unsigned long ptr;
  768. unsigned long ptr_end;
  769. unsigned long name_ptr;
  770. int found_name_len;
  771. int item_size;
  772. int ret;
  773. int match = 0;
  774. path = btrfs_alloc_path();
  775. if (!path)
  776. return -ENOMEM;
  777. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  778. if (ret != 0)
  779. goto out;
  780. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  781. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  782. if (btrfs_find_name_in_ext_backref(path, ref_objectid,
  783. name, namelen, NULL))
  784. match = 1;
  785. goto out;
  786. }
  787. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  788. ptr_end = ptr + item_size;
  789. while (ptr < ptr_end) {
  790. ref = (struct btrfs_inode_ref *)ptr;
  791. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  792. if (found_name_len == namelen) {
  793. name_ptr = (unsigned long)(ref + 1);
  794. ret = memcmp_extent_buffer(path->nodes[0], name,
  795. name_ptr, namelen);
  796. if (ret == 0) {
  797. match = 1;
  798. goto out;
  799. }
  800. }
  801. ptr = (unsigned long)(ref + 1) + found_name_len;
  802. }
  803. out:
  804. btrfs_free_path(path);
  805. return match;
  806. }
  807. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  808. struct btrfs_root *root,
  809. struct btrfs_path *path,
  810. struct btrfs_root *log_root,
  811. struct inode *dir, struct inode *inode,
  812. struct extent_buffer *eb,
  813. u64 inode_objectid, u64 parent_objectid,
  814. u64 ref_index, char *name, int namelen,
  815. int *search_done)
  816. {
  817. int ret;
  818. char *victim_name;
  819. int victim_name_len;
  820. struct extent_buffer *leaf;
  821. struct btrfs_dir_item *di;
  822. struct btrfs_key search_key;
  823. struct btrfs_inode_extref *extref;
  824. again:
  825. /* Search old style refs */
  826. search_key.objectid = inode_objectid;
  827. search_key.type = BTRFS_INODE_REF_KEY;
  828. search_key.offset = parent_objectid;
  829. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  830. if (ret == 0) {
  831. struct btrfs_inode_ref *victim_ref;
  832. unsigned long ptr;
  833. unsigned long ptr_end;
  834. leaf = path->nodes[0];
  835. /* are we trying to overwrite a back ref for the root directory
  836. * if so, just jump out, we're done
  837. */
  838. if (search_key.objectid == search_key.offset)
  839. return 1;
  840. /* check all the names in this back reference to see
  841. * if they are in the log. if so, we allow them to stay
  842. * otherwise they must be unlinked as a conflict
  843. */
  844. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  845. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  846. while (ptr < ptr_end) {
  847. victim_ref = (struct btrfs_inode_ref *)ptr;
  848. victim_name_len = btrfs_inode_ref_name_len(leaf,
  849. victim_ref);
  850. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  851. if (!victim_name)
  852. return -ENOMEM;
  853. read_extent_buffer(leaf, victim_name,
  854. (unsigned long)(victim_ref + 1),
  855. victim_name_len);
  856. if (!backref_in_log(log_root, &search_key,
  857. parent_objectid,
  858. victim_name,
  859. victim_name_len)) {
  860. inc_nlink(inode);
  861. btrfs_release_path(path);
  862. ret = btrfs_unlink_inode(trans, root, dir,
  863. inode, victim_name,
  864. victim_name_len);
  865. kfree(victim_name);
  866. if (ret)
  867. return ret;
  868. ret = btrfs_run_delayed_items(trans, root);
  869. if (ret)
  870. return ret;
  871. *search_done = 1;
  872. goto again;
  873. }
  874. kfree(victim_name);
  875. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  876. }
  877. /*
  878. * NOTE: we have searched root tree and checked the
  879. * coresponding ref, it does not need to check again.
  880. */
  881. *search_done = 1;
  882. }
  883. btrfs_release_path(path);
  884. /* Same search but for extended refs */
  885. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  886. inode_objectid, parent_objectid, 0,
  887. 0);
  888. if (!IS_ERR_OR_NULL(extref)) {
  889. u32 item_size;
  890. u32 cur_offset = 0;
  891. unsigned long base;
  892. struct inode *victim_parent;
  893. leaf = path->nodes[0];
  894. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  895. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  896. while (cur_offset < item_size) {
  897. extref = (struct btrfs_inode_extref *)base + cur_offset;
  898. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  899. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  900. goto next;
  901. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  902. if (!victim_name)
  903. return -ENOMEM;
  904. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  905. victim_name_len);
  906. search_key.objectid = inode_objectid;
  907. search_key.type = BTRFS_INODE_EXTREF_KEY;
  908. search_key.offset = btrfs_extref_hash(parent_objectid,
  909. victim_name,
  910. victim_name_len);
  911. ret = 0;
  912. if (!backref_in_log(log_root, &search_key,
  913. parent_objectid, victim_name,
  914. victim_name_len)) {
  915. ret = -ENOENT;
  916. victim_parent = read_one_inode(root,
  917. parent_objectid);
  918. if (victim_parent) {
  919. inc_nlink(inode);
  920. btrfs_release_path(path);
  921. ret = btrfs_unlink_inode(trans, root,
  922. victim_parent,
  923. inode,
  924. victim_name,
  925. victim_name_len);
  926. if (!ret)
  927. ret = btrfs_run_delayed_items(
  928. trans, root);
  929. }
  930. iput(victim_parent);
  931. kfree(victim_name);
  932. if (ret)
  933. return ret;
  934. *search_done = 1;
  935. goto again;
  936. }
  937. kfree(victim_name);
  938. if (ret)
  939. return ret;
  940. next:
  941. cur_offset += victim_name_len + sizeof(*extref);
  942. }
  943. *search_done = 1;
  944. }
  945. btrfs_release_path(path);
  946. /* look for a conflicting sequence number */
  947. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  948. ref_index, name, namelen, 0);
  949. if (di && !IS_ERR(di)) {
  950. ret = drop_one_dir_item(trans, root, path, dir, di);
  951. if (ret)
  952. return ret;
  953. }
  954. btrfs_release_path(path);
  955. /* look for a conflicing name */
  956. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  957. name, namelen, 0);
  958. if (di && !IS_ERR(di)) {
  959. ret = drop_one_dir_item(trans, root, path, dir, di);
  960. if (ret)
  961. return ret;
  962. }
  963. btrfs_release_path(path);
  964. return 0;
  965. }
  966. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  967. u32 *namelen, char **name, u64 *index,
  968. u64 *parent_objectid)
  969. {
  970. struct btrfs_inode_extref *extref;
  971. extref = (struct btrfs_inode_extref *)ref_ptr;
  972. *namelen = btrfs_inode_extref_name_len(eb, extref);
  973. *name = kmalloc(*namelen, GFP_NOFS);
  974. if (*name == NULL)
  975. return -ENOMEM;
  976. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  977. *namelen);
  978. *index = btrfs_inode_extref_index(eb, extref);
  979. if (parent_objectid)
  980. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  981. return 0;
  982. }
  983. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  984. u32 *namelen, char **name, u64 *index)
  985. {
  986. struct btrfs_inode_ref *ref;
  987. ref = (struct btrfs_inode_ref *)ref_ptr;
  988. *namelen = btrfs_inode_ref_name_len(eb, ref);
  989. *name = kmalloc(*namelen, GFP_NOFS);
  990. if (*name == NULL)
  991. return -ENOMEM;
  992. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  993. *index = btrfs_inode_ref_index(eb, ref);
  994. return 0;
  995. }
  996. /*
  997. * replay one inode back reference item found in the log tree.
  998. * eb, slot and key refer to the buffer and key found in the log tree.
  999. * root is the destination we are replaying into, and path is for temp
  1000. * use by this function. (it should be released on return).
  1001. */
  1002. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1003. struct btrfs_root *root,
  1004. struct btrfs_root *log,
  1005. struct btrfs_path *path,
  1006. struct extent_buffer *eb, int slot,
  1007. struct btrfs_key *key)
  1008. {
  1009. struct inode *dir = NULL;
  1010. struct inode *inode = NULL;
  1011. unsigned long ref_ptr;
  1012. unsigned long ref_end;
  1013. char *name = NULL;
  1014. int namelen;
  1015. int ret;
  1016. int search_done = 0;
  1017. int log_ref_ver = 0;
  1018. u64 parent_objectid;
  1019. u64 inode_objectid;
  1020. u64 ref_index = 0;
  1021. int ref_struct_size;
  1022. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1023. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1024. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1025. struct btrfs_inode_extref *r;
  1026. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1027. log_ref_ver = 1;
  1028. r = (struct btrfs_inode_extref *)ref_ptr;
  1029. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1030. } else {
  1031. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1032. parent_objectid = key->offset;
  1033. }
  1034. inode_objectid = key->objectid;
  1035. /*
  1036. * it is possible that we didn't log all the parent directories
  1037. * for a given inode. If we don't find the dir, just don't
  1038. * copy the back ref in. The link count fixup code will take
  1039. * care of the rest
  1040. */
  1041. dir = read_one_inode(root, parent_objectid);
  1042. if (!dir) {
  1043. ret = -ENOENT;
  1044. goto out;
  1045. }
  1046. inode = read_one_inode(root, inode_objectid);
  1047. if (!inode) {
  1048. ret = -EIO;
  1049. goto out;
  1050. }
  1051. while (ref_ptr < ref_end) {
  1052. if (log_ref_ver) {
  1053. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1054. &ref_index, &parent_objectid);
  1055. /*
  1056. * parent object can change from one array
  1057. * item to another.
  1058. */
  1059. if (!dir)
  1060. dir = read_one_inode(root, parent_objectid);
  1061. if (!dir) {
  1062. ret = -ENOENT;
  1063. goto out;
  1064. }
  1065. } else {
  1066. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1067. &ref_index);
  1068. }
  1069. if (ret)
  1070. goto out;
  1071. /* if we already have a perfect match, we're done */
  1072. if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
  1073. ref_index, name, namelen)) {
  1074. /*
  1075. * look for a conflicting back reference in the
  1076. * metadata. if we find one we have to unlink that name
  1077. * of the file before we add our new link. Later on, we
  1078. * overwrite any existing back reference, and we don't
  1079. * want to create dangling pointers in the directory.
  1080. */
  1081. if (!search_done) {
  1082. ret = __add_inode_ref(trans, root, path, log,
  1083. dir, inode, eb,
  1084. inode_objectid,
  1085. parent_objectid,
  1086. ref_index, name, namelen,
  1087. &search_done);
  1088. if (ret) {
  1089. if (ret == 1)
  1090. ret = 0;
  1091. goto out;
  1092. }
  1093. }
  1094. /* insert our name */
  1095. ret = btrfs_add_link(trans, dir, inode, name, namelen,
  1096. 0, ref_index);
  1097. if (ret)
  1098. goto out;
  1099. btrfs_update_inode(trans, root, inode);
  1100. }
  1101. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1102. kfree(name);
  1103. name = NULL;
  1104. if (log_ref_ver) {
  1105. iput(dir);
  1106. dir = NULL;
  1107. }
  1108. }
  1109. /* finally write the back reference in the inode */
  1110. ret = overwrite_item(trans, root, path, eb, slot, key);
  1111. out:
  1112. btrfs_release_path(path);
  1113. kfree(name);
  1114. iput(dir);
  1115. iput(inode);
  1116. return ret;
  1117. }
  1118. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1119. struct btrfs_root *root, u64 offset)
  1120. {
  1121. int ret;
  1122. ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
  1123. offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
  1124. if (ret > 0)
  1125. ret = btrfs_insert_orphan_item(trans, root, offset);
  1126. return ret;
  1127. }
  1128. static int count_inode_extrefs(struct btrfs_root *root,
  1129. struct inode *inode, struct btrfs_path *path)
  1130. {
  1131. int ret = 0;
  1132. int name_len;
  1133. unsigned int nlink = 0;
  1134. u32 item_size;
  1135. u32 cur_offset = 0;
  1136. u64 inode_objectid = btrfs_ino(inode);
  1137. u64 offset = 0;
  1138. unsigned long ptr;
  1139. struct btrfs_inode_extref *extref;
  1140. struct extent_buffer *leaf;
  1141. while (1) {
  1142. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1143. &extref, &offset);
  1144. if (ret)
  1145. break;
  1146. leaf = path->nodes[0];
  1147. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1148. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1149. while (cur_offset < item_size) {
  1150. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1151. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1152. nlink++;
  1153. cur_offset += name_len + sizeof(*extref);
  1154. }
  1155. offset++;
  1156. btrfs_release_path(path);
  1157. }
  1158. btrfs_release_path(path);
  1159. if (ret < 0)
  1160. return ret;
  1161. return nlink;
  1162. }
  1163. static int count_inode_refs(struct btrfs_root *root,
  1164. struct inode *inode, struct btrfs_path *path)
  1165. {
  1166. int ret;
  1167. struct btrfs_key key;
  1168. unsigned int nlink = 0;
  1169. unsigned long ptr;
  1170. unsigned long ptr_end;
  1171. int name_len;
  1172. u64 ino = btrfs_ino(inode);
  1173. key.objectid = ino;
  1174. key.type = BTRFS_INODE_REF_KEY;
  1175. key.offset = (u64)-1;
  1176. while (1) {
  1177. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1178. if (ret < 0)
  1179. break;
  1180. if (ret > 0) {
  1181. if (path->slots[0] == 0)
  1182. break;
  1183. path->slots[0]--;
  1184. }
  1185. process_slot:
  1186. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1187. path->slots[0]);
  1188. if (key.objectid != ino ||
  1189. key.type != BTRFS_INODE_REF_KEY)
  1190. break;
  1191. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1192. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1193. path->slots[0]);
  1194. while (ptr < ptr_end) {
  1195. struct btrfs_inode_ref *ref;
  1196. ref = (struct btrfs_inode_ref *)ptr;
  1197. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1198. ref);
  1199. ptr = (unsigned long)(ref + 1) + name_len;
  1200. nlink++;
  1201. }
  1202. if (key.offset == 0)
  1203. break;
  1204. if (path->slots[0] > 0) {
  1205. path->slots[0]--;
  1206. goto process_slot;
  1207. }
  1208. key.offset--;
  1209. btrfs_release_path(path);
  1210. }
  1211. btrfs_release_path(path);
  1212. return nlink;
  1213. }
  1214. /*
  1215. * There are a few corners where the link count of the file can't
  1216. * be properly maintained during replay. So, instead of adding
  1217. * lots of complexity to the log code, we just scan the backrefs
  1218. * for any file that has been through replay.
  1219. *
  1220. * The scan will update the link count on the inode to reflect the
  1221. * number of back refs found. If it goes down to zero, the iput
  1222. * will free the inode.
  1223. */
  1224. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1225. struct btrfs_root *root,
  1226. struct inode *inode)
  1227. {
  1228. struct btrfs_path *path;
  1229. int ret;
  1230. u64 nlink = 0;
  1231. u64 ino = btrfs_ino(inode);
  1232. path = btrfs_alloc_path();
  1233. if (!path)
  1234. return -ENOMEM;
  1235. ret = count_inode_refs(root, inode, path);
  1236. if (ret < 0)
  1237. goto out;
  1238. nlink = ret;
  1239. ret = count_inode_extrefs(root, inode, path);
  1240. if (ret == -ENOENT)
  1241. ret = 0;
  1242. if (ret < 0)
  1243. goto out;
  1244. nlink += ret;
  1245. ret = 0;
  1246. if (nlink != inode->i_nlink) {
  1247. set_nlink(inode, nlink);
  1248. btrfs_update_inode(trans, root, inode);
  1249. }
  1250. BTRFS_I(inode)->index_cnt = (u64)-1;
  1251. if (inode->i_nlink == 0) {
  1252. if (S_ISDIR(inode->i_mode)) {
  1253. ret = replay_dir_deletes(trans, root, NULL, path,
  1254. ino, 1);
  1255. if (ret)
  1256. goto out;
  1257. }
  1258. ret = insert_orphan_item(trans, root, ino);
  1259. }
  1260. out:
  1261. btrfs_free_path(path);
  1262. return ret;
  1263. }
  1264. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1265. struct btrfs_root *root,
  1266. struct btrfs_path *path)
  1267. {
  1268. int ret;
  1269. struct btrfs_key key;
  1270. struct inode *inode;
  1271. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1272. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1273. key.offset = (u64)-1;
  1274. while (1) {
  1275. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1276. if (ret < 0)
  1277. break;
  1278. if (ret == 1) {
  1279. if (path->slots[0] == 0)
  1280. break;
  1281. path->slots[0]--;
  1282. }
  1283. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1284. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1285. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1286. break;
  1287. ret = btrfs_del_item(trans, root, path);
  1288. if (ret)
  1289. goto out;
  1290. btrfs_release_path(path);
  1291. inode = read_one_inode(root, key.offset);
  1292. if (!inode)
  1293. return -EIO;
  1294. ret = fixup_inode_link_count(trans, root, inode);
  1295. iput(inode);
  1296. if (ret)
  1297. goto out;
  1298. /*
  1299. * fixup on a directory may create new entries,
  1300. * make sure we always look for the highset possible
  1301. * offset
  1302. */
  1303. key.offset = (u64)-1;
  1304. }
  1305. ret = 0;
  1306. out:
  1307. btrfs_release_path(path);
  1308. return ret;
  1309. }
  1310. /*
  1311. * record a given inode in the fixup dir so we can check its link
  1312. * count when replay is done. The link count is incremented here
  1313. * so the inode won't go away until we check it
  1314. */
  1315. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1316. struct btrfs_root *root,
  1317. struct btrfs_path *path,
  1318. u64 objectid)
  1319. {
  1320. struct btrfs_key key;
  1321. int ret = 0;
  1322. struct inode *inode;
  1323. inode = read_one_inode(root, objectid);
  1324. if (!inode)
  1325. return -EIO;
  1326. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1327. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1328. key.offset = objectid;
  1329. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1330. btrfs_release_path(path);
  1331. if (ret == 0) {
  1332. if (!inode->i_nlink)
  1333. set_nlink(inode, 1);
  1334. else
  1335. inc_nlink(inode);
  1336. ret = btrfs_update_inode(trans, root, inode);
  1337. } else if (ret == -EEXIST) {
  1338. ret = 0;
  1339. } else {
  1340. BUG(); /* Logic Error */
  1341. }
  1342. iput(inode);
  1343. return ret;
  1344. }
  1345. /*
  1346. * when replaying the log for a directory, we only insert names
  1347. * for inodes that actually exist. This means an fsync on a directory
  1348. * does not implicitly fsync all the new files in it
  1349. */
  1350. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1351. struct btrfs_root *root,
  1352. struct btrfs_path *path,
  1353. u64 dirid, u64 index,
  1354. char *name, int name_len, u8 type,
  1355. struct btrfs_key *location)
  1356. {
  1357. struct inode *inode;
  1358. struct inode *dir;
  1359. int ret;
  1360. inode = read_one_inode(root, location->objectid);
  1361. if (!inode)
  1362. return -ENOENT;
  1363. dir = read_one_inode(root, dirid);
  1364. if (!dir) {
  1365. iput(inode);
  1366. return -EIO;
  1367. }
  1368. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1369. /* FIXME, put inode into FIXUP list */
  1370. iput(inode);
  1371. iput(dir);
  1372. return ret;
  1373. }
  1374. /*
  1375. * take a single entry in a log directory item and replay it into
  1376. * the subvolume.
  1377. *
  1378. * if a conflicting item exists in the subdirectory already,
  1379. * the inode it points to is unlinked and put into the link count
  1380. * fix up tree.
  1381. *
  1382. * If a name from the log points to a file or directory that does
  1383. * not exist in the FS, it is skipped. fsyncs on directories
  1384. * do not force down inodes inside that directory, just changes to the
  1385. * names or unlinks in a directory.
  1386. */
  1387. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1388. struct btrfs_root *root,
  1389. struct btrfs_path *path,
  1390. struct extent_buffer *eb,
  1391. struct btrfs_dir_item *di,
  1392. struct btrfs_key *key)
  1393. {
  1394. char *name;
  1395. int name_len;
  1396. struct btrfs_dir_item *dst_di;
  1397. struct btrfs_key found_key;
  1398. struct btrfs_key log_key;
  1399. struct inode *dir;
  1400. u8 log_type;
  1401. int exists;
  1402. int ret = 0;
  1403. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1404. dir = read_one_inode(root, key->objectid);
  1405. if (!dir)
  1406. return -EIO;
  1407. name_len = btrfs_dir_name_len(eb, di);
  1408. name = kmalloc(name_len, GFP_NOFS);
  1409. if (!name) {
  1410. ret = -ENOMEM;
  1411. goto out;
  1412. }
  1413. log_type = btrfs_dir_type(eb, di);
  1414. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1415. name_len);
  1416. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1417. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1418. if (exists == 0)
  1419. exists = 1;
  1420. else
  1421. exists = 0;
  1422. btrfs_release_path(path);
  1423. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1424. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1425. name, name_len, 1);
  1426. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1427. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1428. key->objectid,
  1429. key->offset, name,
  1430. name_len, 1);
  1431. } else {
  1432. /* Corruption */
  1433. ret = -EINVAL;
  1434. goto out;
  1435. }
  1436. if (IS_ERR_OR_NULL(dst_di)) {
  1437. /* we need a sequence number to insert, so we only
  1438. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1439. */
  1440. if (key->type != BTRFS_DIR_INDEX_KEY)
  1441. goto out;
  1442. goto insert;
  1443. }
  1444. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1445. /* the existing item matches the logged item */
  1446. if (found_key.objectid == log_key.objectid &&
  1447. found_key.type == log_key.type &&
  1448. found_key.offset == log_key.offset &&
  1449. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1450. goto out;
  1451. }
  1452. /*
  1453. * don't drop the conflicting directory entry if the inode
  1454. * for the new entry doesn't exist
  1455. */
  1456. if (!exists)
  1457. goto out;
  1458. ret = drop_one_dir_item(trans, root, path, dir, dst_di);
  1459. if (ret)
  1460. goto out;
  1461. if (key->type == BTRFS_DIR_INDEX_KEY)
  1462. goto insert;
  1463. out:
  1464. btrfs_release_path(path);
  1465. if (!ret && update_size) {
  1466. btrfs_i_size_write(dir, dir->i_size + name_len * 2);
  1467. ret = btrfs_update_inode(trans, root, dir);
  1468. }
  1469. kfree(name);
  1470. iput(dir);
  1471. return ret;
  1472. insert:
  1473. btrfs_release_path(path);
  1474. ret = insert_one_name(trans, root, path, key->objectid, key->offset,
  1475. name, name_len, log_type, &log_key);
  1476. if (ret && ret != -ENOENT)
  1477. goto out;
  1478. update_size = false;
  1479. ret = 0;
  1480. goto out;
  1481. }
  1482. /*
  1483. * find all the names in a directory item and reconcile them into
  1484. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1485. * one name in a directory item, but the same code gets used for
  1486. * both directory index types
  1487. */
  1488. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1489. struct btrfs_root *root,
  1490. struct btrfs_path *path,
  1491. struct extent_buffer *eb, int slot,
  1492. struct btrfs_key *key)
  1493. {
  1494. int ret;
  1495. u32 item_size = btrfs_item_size_nr(eb, slot);
  1496. struct btrfs_dir_item *di;
  1497. int name_len;
  1498. unsigned long ptr;
  1499. unsigned long ptr_end;
  1500. ptr = btrfs_item_ptr_offset(eb, slot);
  1501. ptr_end = ptr + item_size;
  1502. while (ptr < ptr_end) {
  1503. di = (struct btrfs_dir_item *)ptr;
  1504. if (verify_dir_item(root, eb, di))
  1505. return -EIO;
  1506. name_len = btrfs_dir_name_len(eb, di);
  1507. ret = replay_one_name(trans, root, path, eb, di, key);
  1508. if (ret)
  1509. return ret;
  1510. ptr = (unsigned long)(di + 1);
  1511. ptr += name_len;
  1512. }
  1513. return 0;
  1514. }
  1515. /*
  1516. * directory replay has two parts. There are the standard directory
  1517. * items in the log copied from the subvolume, and range items
  1518. * created in the log while the subvolume was logged.
  1519. *
  1520. * The range items tell us which parts of the key space the log
  1521. * is authoritative for. During replay, if a key in the subvolume
  1522. * directory is in a logged range item, but not actually in the log
  1523. * that means it was deleted from the directory before the fsync
  1524. * and should be removed.
  1525. */
  1526. static noinline int find_dir_range(struct btrfs_root *root,
  1527. struct btrfs_path *path,
  1528. u64 dirid, int key_type,
  1529. u64 *start_ret, u64 *end_ret)
  1530. {
  1531. struct btrfs_key key;
  1532. u64 found_end;
  1533. struct btrfs_dir_log_item *item;
  1534. int ret;
  1535. int nritems;
  1536. if (*start_ret == (u64)-1)
  1537. return 1;
  1538. key.objectid = dirid;
  1539. key.type = key_type;
  1540. key.offset = *start_ret;
  1541. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1542. if (ret < 0)
  1543. goto out;
  1544. if (ret > 0) {
  1545. if (path->slots[0] == 0)
  1546. goto out;
  1547. path->slots[0]--;
  1548. }
  1549. if (ret != 0)
  1550. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1551. if (key.type != key_type || key.objectid != dirid) {
  1552. ret = 1;
  1553. goto next;
  1554. }
  1555. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1556. struct btrfs_dir_log_item);
  1557. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1558. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1559. ret = 0;
  1560. *start_ret = key.offset;
  1561. *end_ret = found_end;
  1562. goto out;
  1563. }
  1564. ret = 1;
  1565. next:
  1566. /* check the next slot in the tree to see if it is a valid item */
  1567. nritems = btrfs_header_nritems(path->nodes[0]);
  1568. if (path->slots[0] >= nritems) {
  1569. ret = btrfs_next_leaf(root, path);
  1570. if (ret)
  1571. goto out;
  1572. } else {
  1573. path->slots[0]++;
  1574. }
  1575. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1576. if (key.type != key_type || key.objectid != dirid) {
  1577. ret = 1;
  1578. goto out;
  1579. }
  1580. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1581. struct btrfs_dir_log_item);
  1582. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1583. *start_ret = key.offset;
  1584. *end_ret = found_end;
  1585. ret = 0;
  1586. out:
  1587. btrfs_release_path(path);
  1588. return ret;
  1589. }
  1590. /*
  1591. * this looks for a given directory item in the log. If the directory
  1592. * item is not in the log, the item is removed and the inode it points
  1593. * to is unlinked
  1594. */
  1595. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1596. struct btrfs_root *root,
  1597. struct btrfs_root *log,
  1598. struct btrfs_path *path,
  1599. struct btrfs_path *log_path,
  1600. struct inode *dir,
  1601. struct btrfs_key *dir_key)
  1602. {
  1603. int ret;
  1604. struct extent_buffer *eb;
  1605. int slot;
  1606. u32 item_size;
  1607. struct btrfs_dir_item *di;
  1608. struct btrfs_dir_item *log_di;
  1609. int name_len;
  1610. unsigned long ptr;
  1611. unsigned long ptr_end;
  1612. char *name;
  1613. struct inode *inode;
  1614. struct btrfs_key location;
  1615. again:
  1616. eb = path->nodes[0];
  1617. slot = path->slots[0];
  1618. item_size = btrfs_item_size_nr(eb, slot);
  1619. ptr = btrfs_item_ptr_offset(eb, slot);
  1620. ptr_end = ptr + item_size;
  1621. while (ptr < ptr_end) {
  1622. di = (struct btrfs_dir_item *)ptr;
  1623. if (verify_dir_item(root, eb, di)) {
  1624. ret = -EIO;
  1625. goto out;
  1626. }
  1627. name_len = btrfs_dir_name_len(eb, di);
  1628. name = kmalloc(name_len, GFP_NOFS);
  1629. if (!name) {
  1630. ret = -ENOMEM;
  1631. goto out;
  1632. }
  1633. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1634. name_len);
  1635. log_di = NULL;
  1636. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1637. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1638. dir_key->objectid,
  1639. name, name_len, 0);
  1640. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1641. log_di = btrfs_lookup_dir_index_item(trans, log,
  1642. log_path,
  1643. dir_key->objectid,
  1644. dir_key->offset,
  1645. name, name_len, 0);
  1646. }
  1647. if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
  1648. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1649. btrfs_release_path(path);
  1650. btrfs_release_path(log_path);
  1651. inode = read_one_inode(root, location.objectid);
  1652. if (!inode) {
  1653. kfree(name);
  1654. return -EIO;
  1655. }
  1656. ret = link_to_fixup_dir(trans, root,
  1657. path, location.objectid);
  1658. if (ret) {
  1659. kfree(name);
  1660. iput(inode);
  1661. goto out;
  1662. }
  1663. inc_nlink(inode);
  1664. ret = btrfs_unlink_inode(trans, root, dir, inode,
  1665. name, name_len);
  1666. if (!ret)
  1667. ret = btrfs_run_delayed_items(trans, root);
  1668. kfree(name);
  1669. iput(inode);
  1670. if (ret)
  1671. goto out;
  1672. /* there might still be more names under this key
  1673. * check and repeat if required
  1674. */
  1675. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1676. 0, 0);
  1677. if (ret == 0)
  1678. goto again;
  1679. ret = 0;
  1680. goto out;
  1681. } else if (IS_ERR(log_di)) {
  1682. kfree(name);
  1683. return PTR_ERR(log_di);
  1684. }
  1685. btrfs_release_path(log_path);
  1686. kfree(name);
  1687. ptr = (unsigned long)(di + 1);
  1688. ptr += name_len;
  1689. }
  1690. ret = 0;
  1691. out:
  1692. btrfs_release_path(path);
  1693. btrfs_release_path(log_path);
  1694. return ret;
  1695. }
  1696. /*
  1697. * deletion replay happens before we copy any new directory items
  1698. * out of the log or out of backreferences from inodes. It
  1699. * scans the log to find ranges of keys that log is authoritative for,
  1700. * and then scans the directory to find items in those ranges that are
  1701. * not present in the log.
  1702. *
  1703. * Anything we don't find in the log is unlinked and removed from the
  1704. * directory.
  1705. */
  1706. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1707. struct btrfs_root *root,
  1708. struct btrfs_root *log,
  1709. struct btrfs_path *path,
  1710. u64 dirid, int del_all)
  1711. {
  1712. u64 range_start;
  1713. u64 range_end;
  1714. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1715. int ret = 0;
  1716. struct btrfs_key dir_key;
  1717. struct btrfs_key found_key;
  1718. struct btrfs_path *log_path;
  1719. struct inode *dir;
  1720. dir_key.objectid = dirid;
  1721. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1722. log_path = btrfs_alloc_path();
  1723. if (!log_path)
  1724. return -ENOMEM;
  1725. dir = read_one_inode(root, dirid);
  1726. /* it isn't an error if the inode isn't there, that can happen
  1727. * because we replay the deletes before we copy in the inode item
  1728. * from the log
  1729. */
  1730. if (!dir) {
  1731. btrfs_free_path(log_path);
  1732. return 0;
  1733. }
  1734. again:
  1735. range_start = 0;
  1736. range_end = 0;
  1737. while (1) {
  1738. if (del_all)
  1739. range_end = (u64)-1;
  1740. else {
  1741. ret = find_dir_range(log, path, dirid, key_type,
  1742. &range_start, &range_end);
  1743. if (ret != 0)
  1744. break;
  1745. }
  1746. dir_key.offset = range_start;
  1747. while (1) {
  1748. int nritems;
  1749. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  1750. 0, 0);
  1751. if (ret < 0)
  1752. goto out;
  1753. nritems = btrfs_header_nritems(path->nodes[0]);
  1754. if (path->slots[0] >= nritems) {
  1755. ret = btrfs_next_leaf(root, path);
  1756. if (ret)
  1757. break;
  1758. }
  1759. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1760. path->slots[0]);
  1761. if (found_key.objectid != dirid ||
  1762. found_key.type != dir_key.type)
  1763. goto next_type;
  1764. if (found_key.offset > range_end)
  1765. break;
  1766. ret = check_item_in_log(trans, root, log, path,
  1767. log_path, dir,
  1768. &found_key);
  1769. if (ret)
  1770. goto out;
  1771. if (found_key.offset == (u64)-1)
  1772. break;
  1773. dir_key.offset = found_key.offset + 1;
  1774. }
  1775. btrfs_release_path(path);
  1776. if (range_end == (u64)-1)
  1777. break;
  1778. range_start = range_end + 1;
  1779. }
  1780. next_type:
  1781. ret = 0;
  1782. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  1783. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  1784. dir_key.type = BTRFS_DIR_INDEX_KEY;
  1785. btrfs_release_path(path);
  1786. goto again;
  1787. }
  1788. out:
  1789. btrfs_release_path(path);
  1790. btrfs_free_path(log_path);
  1791. iput(dir);
  1792. return ret;
  1793. }
  1794. /*
  1795. * the process_func used to replay items from the log tree. This
  1796. * gets called in two different stages. The first stage just looks
  1797. * for inodes and makes sure they are all copied into the subvolume.
  1798. *
  1799. * The second stage copies all the other item types from the log into
  1800. * the subvolume. The two stage approach is slower, but gets rid of
  1801. * lots of complexity around inodes referencing other inodes that exist
  1802. * only in the log (references come from either directory items or inode
  1803. * back refs).
  1804. */
  1805. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  1806. struct walk_control *wc, u64 gen)
  1807. {
  1808. int nritems;
  1809. struct btrfs_path *path;
  1810. struct btrfs_root *root = wc->replay_dest;
  1811. struct btrfs_key key;
  1812. int level;
  1813. int i;
  1814. int ret;
  1815. ret = btrfs_read_buffer(eb, gen);
  1816. if (ret)
  1817. return ret;
  1818. level = btrfs_header_level(eb);
  1819. if (level != 0)
  1820. return 0;
  1821. path = btrfs_alloc_path();
  1822. if (!path)
  1823. return -ENOMEM;
  1824. nritems = btrfs_header_nritems(eb);
  1825. for (i = 0; i < nritems; i++) {
  1826. btrfs_item_key_to_cpu(eb, &key, i);
  1827. /* inode keys are done during the first stage */
  1828. if (key.type == BTRFS_INODE_ITEM_KEY &&
  1829. wc->stage == LOG_WALK_REPLAY_INODES) {
  1830. struct btrfs_inode_item *inode_item;
  1831. u32 mode;
  1832. inode_item = btrfs_item_ptr(eb, i,
  1833. struct btrfs_inode_item);
  1834. mode = btrfs_inode_mode(eb, inode_item);
  1835. if (S_ISDIR(mode)) {
  1836. ret = replay_dir_deletes(wc->trans,
  1837. root, log, path, key.objectid, 0);
  1838. if (ret)
  1839. break;
  1840. }
  1841. ret = overwrite_item(wc->trans, root, path,
  1842. eb, i, &key);
  1843. if (ret)
  1844. break;
  1845. /* for regular files, make sure corresponding
  1846. * orhpan item exist. extents past the new EOF
  1847. * will be truncated later by orphan cleanup.
  1848. */
  1849. if (S_ISREG(mode)) {
  1850. ret = insert_orphan_item(wc->trans, root,
  1851. key.objectid);
  1852. if (ret)
  1853. break;
  1854. }
  1855. ret = link_to_fixup_dir(wc->trans, root,
  1856. path, key.objectid);
  1857. if (ret)
  1858. break;
  1859. }
  1860. if (key.type == BTRFS_DIR_INDEX_KEY &&
  1861. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  1862. ret = replay_one_dir_item(wc->trans, root, path,
  1863. eb, i, &key);
  1864. if (ret)
  1865. break;
  1866. }
  1867. if (wc->stage < LOG_WALK_REPLAY_ALL)
  1868. continue;
  1869. /* these keys are simply copied */
  1870. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  1871. ret = overwrite_item(wc->trans, root, path,
  1872. eb, i, &key);
  1873. if (ret)
  1874. break;
  1875. } else if (key.type == BTRFS_INODE_REF_KEY ||
  1876. key.type == BTRFS_INODE_EXTREF_KEY) {
  1877. ret = add_inode_ref(wc->trans, root, log, path,
  1878. eb, i, &key);
  1879. if (ret && ret != -ENOENT)
  1880. break;
  1881. ret = 0;
  1882. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  1883. ret = replay_one_extent(wc->trans, root, path,
  1884. eb, i, &key);
  1885. if (ret)
  1886. break;
  1887. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  1888. ret = replay_one_dir_item(wc->trans, root, path,
  1889. eb, i, &key);
  1890. if (ret)
  1891. break;
  1892. }
  1893. }
  1894. btrfs_free_path(path);
  1895. return ret;
  1896. }
  1897. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  1898. struct btrfs_root *root,
  1899. struct btrfs_path *path, int *level,
  1900. struct walk_control *wc)
  1901. {
  1902. u64 root_owner;
  1903. u64 bytenr;
  1904. u64 ptr_gen;
  1905. struct extent_buffer *next;
  1906. struct extent_buffer *cur;
  1907. struct extent_buffer *parent;
  1908. u32 blocksize;
  1909. int ret = 0;
  1910. WARN_ON(*level < 0);
  1911. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1912. while (*level > 0) {
  1913. WARN_ON(*level < 0);
  1914. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1915. cur = path->nodes[*level];
  1916. WARN_ON(btrfs_header_level(cur) != *level);
  1917. if (path->slots[*level] >=
  1918. btrfs_header_nritems(cur))
  1919. break;
  1920. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1921. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  1922. blocksize = root->nodesize;
  1923. parent = path->nodes[*level];
  1924. root_owner = btrfs_header_owner(parent);
  1925. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  1926. if (!next)
  1927. return -ENOMEM;
  1928. if (*level == 1) {
  1929. ret = wc->process_func(root, next, wc, ptr_gen);
  1930. if (ret) {
  1931. free_extent_buffer(next);
  1932. return ret;
  1933. }
  1934. path->slots[*level]++;
  1935. if (wc->free) {
  1936. ret = btrfs_read_buffer(next, ptr_gen);
  1937. if (ret) {
  1938. free_extent_buffer(next);
  1939. return ret;
  1940. }
  1941. if (trans) {
  1942. btrfs_tree_lock(next);
  1943. btrfs_set_lock_blocking(next);
  1944. clean_tree_block(trans, root, next);
  1945. btrfs_wait_tree_block_writeback(next);
  1946. btrfs_tree_unlock(next);
  1947. }
  1948. WARN_ON(root_owner !=
  1949. BTRFS_TREE_LOG_OBJECTID);
  1950. ret = btrfs_free_and_pin_reserved_extent(root,
  1951. bytenr, blocksize);
  1952. if (ret) {
  1953. free_extent_buffer(next);
  1954. return ret;
  1955. }
  1956. }
  1957. free_extent_buffer(next);
  1958. continue;
  1959. }
  1960. ret = btrfs_read_buffer(next, ptr_gen);
  1961. if (ret) {
  1962. free_extent_buffer(next);
  1963. return ret;
  1964. }
  1965. WARN_ON(*level <= 0);
  1966. if (path->nodes[*level-1])
  1967. free_extent_buffer(path->nodes[*level-1]);
  1968. path->nodes[*level-1] = next;
  1969. *level = btrfs_header_level(next);
  1970. path->slots[*level] = 0;
  1971. cond_resched();
  1972. }
  1973. WARN_ON(*level < 0);
  1974. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1975. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  1976. cond_resched();
  1977. return 0;
  1978. }
  1979. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  1980. struct btrfs_root *root,
  1981. struct btrfs_path *path, int *level,
  1982. struct walk_control *wc)
  1983. {
  1984. u64 root_owner;
  1985. int i;
  1986. int slot;
  1987. int ret;
  1988. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1989. slot = path->slots[i];
  1990. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  1991. path->slots[i]++;
  1992. *level = i;
  1993. WARN_ON(*level == 0);
  1994. return 0;
  1995. } else {
  1996. struct extent_buffer *parent;
  1997. if (path->nodes[*level] == root->node)
  1998. parent = path->nodes[*level];
  1999. else
  2000. parent = path->nodes[*level + 1];
  2001. root_owner = btrfs_header_owner(parent);
  2002. ret = wc->process_func(root, path->nodes[*level], wc,
  2003. btrfs_header_generation(path->nodes[*level]));
  2004. if (ret)
  2005. return ret;
  2006. if (wc->free) {
  2007. struct extent_buffer *next;
  2008. next = path->nodes[*level];
  2009. if (trans) {
  2010. btrfs_tree_lock(next);
  2011. btrfs_set_lock_blocking(next);
  2012. clean_tree_block(trans, root, next);
  2013. btrfs_wait_tree_block_writeback(next);
  2014. btrfs_tree_unlock(next);
  2015. }
  2016. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2017. ret = btrfs_free_and_pin_reserved_extent(root,
  2018. path->nodes[*level]->start,
  2019. path->nodes[*level]->len);
  2020. if (ret)
  2021. return ret;
  2022. }
  2023. free_extent_buffer(path->nodes[*level]);
  2024. path->nodes[*level] = NULL;
  2025. *level = i + 1;
  2026. }
  2027. }
  2028. return 1;
  2029. }
  2030. /*
  2031. * drop the reference count on the tree rooted at 'snap'. This traverses
  2032. * the tree freeing any blocks that have a ref count of zero after being
  2033. * decremented.
  2034. */
  2035. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2036. struct btrfs_root *log, struct walk_control *wc)
  2037. {
  2038. int ret = 0;
  2039. int wret;
  2040. int level;
  2041. struct btrfs_path *path;
  2042. int orig_level;
  2043. path = btrfs_alloc_path();
  2044. if (!path)
  2045. return -ENOMEM;
  2046. level = btrfs_header_level(log->node);
  2047. orig_level = level;
  2048. path->nodes[level] = log->node;
  2049. extent_buffer_get(log->node);
  2050. path->slots[level] = 0;
  2051. while (1) {
  2052. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2053. if (wret > 0)
  2054. break;
  2055. if (wret < 0) {
  2056. ret = wret;
  2057. goto out;
  2058. }
  2059. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2060. if (wret > 0)
  2061. break;
  2062. if (wret < 0) {
  2063. ret = wret;
  2064. goto out;
  2065. }
  2066. }
  2067. /* was the root node processed? if not, catch it here */
  2068. if (path->nodes[orig_level]) {
  2069. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2070. btrfs_header_generation(path->nodes[orig_level]));
  2071. if (ret)
  2072. goto out;
  2073. if (wc->free) {
  2074. struct extent_buffer *next;
  2075. next = path->nodes[orig_level];
  2076. if (trans) {
  2077. btrfs_tree_lock(next);
  2078. btrfs_set_lock_blocking(next);
  2079. clean_tree_block(trans, log, next);
  2080. btrfs_wait_tree_block_writeback(next);
  2081. btrfs_tree_unlock(next);
  2082. }
  2083. WARN_ON(log->root_key.objectid !=
  2084. BTRFS_TREE_LOG_OBJECTID);
  2085. ret = btrfs_free_and_pin_reserved_extent(log, next->start,
  2086. next->len);
  2087. if (ret)
  2088. goto out;
  2089. }
  2090. }
  2091. out:
  2092. btrfs_free_path(path);
  2093. return ret;
  2094. }
  2095. /*
  2096. * helper function to update the item for a given subvolumes log root
  2097. * in the tree of log roots
  2098. */
  2099. static int update_log_root(struct btrfs_trans_handle *trans,
  2100. struct btrfs_root *log)
  2101. {
  2102. int ret;
  2103. if (log->log_transid == 1) {
  2104. /* insert root item on the first sync */
  2105. ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
  2106. &log->root_key, &log->root_item);
  2107. } else {
  2108. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  2109. &log->root_key, &log->root_item);
  2110. }
  2111. return ret;
  2112. }
  2113. static void wait_log_commit(struct btrfs_trans_handle *trans,
  2114. struct btrfs_root *root, int transid)
  2115. {
  2116. DEFINE_WAIT(wait);
  2117. int index = transid % 2;
  2118. /*
  2119. * we only allow two pending log transactions at a time,
  2120. * so we know that if ours is more than 2 older than the
  2121. * current transaction, we're done
  2122. */
  2123. do {
  2124. prepare_to_wait(&root->log_commit_wait[index],
  2125. &wait, TASK_UNINTERRUPTIBLE);
  2126. mutex_unlock(&root->log_mutex);
  2127. if (root->log_transid_committed < transid &&
  2128. atomic_read(&root->log_commit[index]))
  2129. schedule();
  2130. finish_wait(&root->log_commit_wait[index], &wait);
  2131. mutex_lock(&root->log_mutex);
  2132. } while (root->log_transid_committed < transid &&
  2133. atomic_read(&root->log_commit[index]));
  2134. }
  2135. static void wait_for_writer(struct btrfs_trans_handle *trans,
  2136. struct btrfs_root *root)
  2137. {
  2138. DEFINE_WAIT(wait);
  2139. while (atomic_read(&root->log_writers)) {
  2140. prepare_to_wait(&root->log_writer_wait,
  2141. &wait, TASK_UNINTERRUPTIBLE);
  2142. mutex_unlock(&root->log_mutex);
  2143. if (atomic_read(&root->log_writers))
  2144. schedule();
  2145. mutex_lock(&root->log_mutex);
  2146. finish_wait(&root->log_writer_wait, &wait);
  2147. }
  2148. }
  2149. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2150. struct btrfs_log_ctx *ctx)
  2151. {
  2152. if (!ctx)
  2153. return;
  2154. mutex_lock(&root->log_mutex);
  2155. list_del_init(&ctx->list);
  2156. mutex_unlock(&root->log_mutex);
  2157. }
  2158. /*
  2159. * Invoked in log mutex context, or be sure there is no other task which
  2160. * can access the list.
  2161. */
  2162. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2163. int index, int error)
  2164. {
  2165. struct btrfs_log_ctx *ctx;
  2166. if (!error) {
  2167. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2168. return;
  2169. }
  2170. list_for_each_entry(ctx, &root->log_ctxs[index], list)
  2171. ctx->log_ret = error;
  2172. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2173. }
  2174. /*
  2175. * btrfs_sync_log does sends a given tree log down to the disk and
  2176. * updates the super blocks to record it. When this call is done,
  2177. * you know that any inodes previously logged are safely on disk only
  2178. * if it returns 0.
  2179. *
  2180. * Any other return value means you need to call btrfs_commit_transaction.
  2181. * Some of the edge cases for fsyncing directories that have had unlinks
  2182. * or renames done in the past mean that sometimes the only safe
  2183. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2184. * that has happened.
  2185. */
  2186. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2187. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2188. {
  2189. int index1;
  2190. int index2;
  2191. int mark;
  2192. int ret;
  2193. struct btrfs_root *log = root->log_root;
  2194. struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
  2195. int log_transid = 0;
  2196. struct btrfs_log_ctx root_log_ctx;
  2197. struct blk_plug plug;
  2198. mutex_lock(&root->log_mutex);
  2199. log_transid = ctx->log_transid;
  2200. if (root->log_transid_committed >= log_transid) {
  2201. mutex_unlock(&root->log_mutex);
  2202. return ctx->log_ret;
  2203. }
  2204. index1 = log_transid % 2;
  2205. if (atomic_read(&root->log_commit[index1])) {
  2206. wait_log_commit(trans, root, log_transid);
  2207. mutex_unlock(&root->log_mutex);
  2208. return ctx->log_ret;
  2209. }
  2210. ASSERT(log_transid == root->log_transid);
  2211. atomic_set(&root->log_commit[index1], 1);
  2212. /* wait for previous tree log sync to complete */
  2213. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2214. wait_log_commit(trans, root, log_transid - 1);
  2215. while (1) {
  2216. int batch = atomic_read(&root->log_batch);
  2217. /* when we're on an ssd, just kick the log commit out */
  2218. if (!btrfs_test_opt(root, SSD) &&
  2219. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2220. mutex_unlock(&root->log_mutex);
  2221. schedule_timeout_uninterruptible(1);
  2222. mutex_lock(&root->log_mutex);
  2223. }
  2224. wait_for_writer(trans, root);
  2225. if (batch == atomic_read(&root->log_batch))
  2226. break;
  2227. }
  2228. /* bail out if we need to do a full commit */
  2229. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  2230. ret = -EAGAIN;
  2231. btrfs_free_logged_extents(log, log_transid);
  2232. mutex_unlock(&root->log_mutex);
  2233. goto out;
  2234. }
  2235. if (log_transid % 2 == 0)
  2236. mark = EXTENT_DIRTY;
  2237. else
  2238. mark = EXTENT_NEW;
  2239. /* we start IO on all the marked extents here, but we don't actually
  2240. * wait for them until later.
  2241. */
  2242. blk_start_plug(&plug);
  2243. ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
  2244. if (ret) {
  2245. blk_finish_plug(&plug);
  2246. btrfs_abort_transaction(trans, root, ret);
  2247. btrfs_free_logged_extents(log, log_transid);
  2248. btrfs_set_log_full_commit(root->fs_info, trans);
  2249. mutex_unlock(&root->log_mutex);
  2250. goto out;
  2251. }
  2252. btrfs_set_root_node(&log->root_item, log->node);
  2253. root->log_transid++;
  2254. log->log_transid = root->log_transid;
  2255. root->log_start_pid = 0;
  2256. /*
  2257. * IO has been started, blocks of the log tree have WRITTEN flag set
  2258. * in their headers. new modifications of the log will be written to
  2259. * new positions. so it's safe to allow log writers to go in.
  2260. */
  2261. mutex_unlock(&root->log_mutex);
  2262. btrfs_init_log_ctx(&root_log_ctx);
  2263. mutex_lock(&log_root_tree->log_mutex);
  2264. atomic_inc(&log_root_tree->log_batch);
  2265. atomic_inc(&log_root_tree->log_writers);
  2266. index2 = log_root_tree->log_transid % 2;
  2267. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2268. root_log_ctx.log_transid = log_root_tree->log_transid;
  2269. mutex_unlock(&log_root_tree->log_mutex);
  2270. ret = update_log_root(trans, log);
  2271. mutex_lock(&log_root_tree->log_mutex);
  2272. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2273. smp_mb();
  2274. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2275. wake_up(&log_root_tree->log_writer_wait);
  2276. }
  2277. if (ret) {
  2278. if (!list_empty(&root_log_ctx.list))
  2279. list_del_init(&root_log_ctx.list);
  2280. blk_finish_plug(&plug);
  2281. btrfs_set_log_full_commit(root->fs_info, trans);
  2282. if (ret != -ENOSPC) {
  2283. btrfs_abort_transaction(trans, root, ret);
  2284. mutex_unlock(&log_root_tree->log_mutex);
  2285. goto out;
  2286. }
  2287. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2288. btrfs_free_logged_extents(log, log_transid);
  2289. mutex_unlock(&log_root_tree->log_mutex);
  2290. ret = -EAGAIN;
  2291. goto out;
  2292. }
  2293. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2294. mutex_unlock(&log_root_tree->log_mutex);
  2295. ret = root_log_ctx.log_ret;
  2296. goto out;
  2297. }
  2298. index2 = root_log_ctx.log_transid % 2;
  2299. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2300. blk_finish_plug(&plug);
  2301. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2302. wait_log_commit(trans, log_root_tree,
  2303. root_log_ctx.log_transid);
  2304. btrfs_free_logged_extents(log, log_transid);
  2305. mutex_unlock(&log_root_tree->log_mutex);
  2306. ret = root_log_ctx.log_ret;
  2307. goto out;
  2308. }
  2309. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2310. atomic_set(&log_root_tree->log_commit[index2], 1);
  2311. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2312. wait_log_commit(trans, log_root_tree,
  2313. root_log_ctx.log_transid - 1);
  2314. }
  2315. wait_for_writer(trans, log_root_tree);
  2316. /*
  2317. * now that we've moved on to the tree of log tree roots,
  2318. * check the full commit flag again
  2319. */
  2320. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  2321. blk_finish_plug(&plug);
  2322. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2323. btrfs_free_logged_extents(log, log_transid);
  2324. mutex_unlock(&log_root_tree->log_mutex);
  2325. ret = -EAGAIN;
  2326. goto out_wake_log_root;
  2327. }
  2328. ret = btrfs_write_marked_extents(log_root_tree,
  2329. &log_root_tree->dirty_log_pages,
  2330. EXTENT_DIRTY | EXTENT_NEW);
  2331. blk_finish_plug(&plug);
  2332. if (ret) {
  2333. btrfs_set_log_full_commit(root->fs_info, trans);
  2334. btrfs_abort_transaction(trans, root, ret);
  2335. btrfs_free_logged_extents(log, log_transid);
  2336. mutex_unlock(&log_root_tree->log_mutex);
  2337. goto out_wake_log_root;
  2338. }
  2339. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2340. btrfs_wait_marked_extents(log_root_tree,
  2341. &log_root_tree->dirty_log_pages,
  2342. EXTENT_NEW | EXTENT_DIRTY);
  2343. btrfs_wait_logged_extents(log, log_transid);
  2344. btrfs_set_super_log_root(root->fs_info->super_for_commit,
  2345. log_root_tree->node->start);
  2346. btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
  2347. btrfs_header_level(log_root_tree->node));
  2348. log_root_tree->log_transid++;
  2349. mutex_unlock(&log_root_tree->log_mutex);
  2350. /*
  2351. * nobody else is going to jump in and write the the ctree
  2352. * super here because the log_commit atomic below is protecting
  2353. * us. We must be called with a transaction handle pinning
  2354. * the running transaction open, so a full commit can't hop
  2355. * in and cause problems either.
  2356. */
  2357. ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
  2358. if (ret) {
  2359. btrfs_set_log_full_commit(root->fs_info, trans);
  2360. btrfs_abort_transaction(trans, root, ret);
  2361. goto out_wake_log_root;
  2362. }
  2363. mutex_lock(&root->log_mutex);
  2364. if (root->last_log_commit < log_transid)
  2365. root->last_log_commit = log_transid;
  2366. mutex_unlock(&root->log_mutex);
  2367. out_wake_log_root:
  2368. /*
  2369. * We needn't get log_mutex here because we are sure all
  2370. * the other tasks are blocked.
  2371. */
  2372. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2373. mutex_lock(&log_root_tree->log_mutex);
  2374. log_root_tree->log_transid_committed++;
  2375. atomic_set(&log_root_tree->log_commit[index2], 0);
  2376. mutex_unlock(&log_root_tree->log_mutex);
  2377. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2378. wake_up(&log_root_tree->log_commit_wait[index2]);
  2379. out:
  2380. /* See above. */
  2381. btrfs_remove_all_log_ctxs(root, index1, ret);
  2382. mutex_lock(&root->log_mutex);
  2383. root->log_transid_committed++;
  2384. atomic_set(&root->log_commit[index1], 0);
  2385. mutex_unlock(&root->log_mutex);
  2386. if (waitqueue_active(&root->log_commit_wait[index1]))
  2387. wake_up(&root->log_commit_wait[index1]);
  2388. return ret;
  2389. }
  2390. static void free_log_tree(struct btrfs_trans_handle *trans,
  2391. struct btrfs_root *log)
  2392. {
  2393. int ret;
  2394. u64 start;
  2395. u64 end;
  2396. struct walk_control wc = {
  2397. .free = 1,
  2398. .process_func = process_one_buffer
  2399. };
  2400. ret = walk_log_tree(trans, log, &wc);
  2401. /* I don't think this can happen but just in case */
  2402. if (ret)
  2403. btrfs_abort_transaction(trans, log, ret);
  2404. while (1) {
  2405. ret = find_first_extent_bit(&log->dirty_log_pages,
  2406. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2407. NULL);
  2408. if (ret)
  2409. break;
  2410. clear_extent_bits(&log->dirty_log_pages, start, end,
  2411. EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
  2412. }
  2413. /*
  2414. * We may have short-circuited the log tree with the full commit logic
  2415. * and left ordered extents on our list, so clear these out to keep us
  2416. * from leaking inodes and memory.
  2417. */
  2418. btrfs_free_logged_extents(log, 0);
  2419. btrfs_free_logged_extents(log, 1);
  2420. free_extent_buffer(log->node);
  2421. kfree(log);
  2422. }
  2423. /*
  2424. * free all the extents used by the tree log. This should be called
  2425. * at commit time of the full transaction
  2426. */
  2427. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2428. {
  2429. if (root->log_root) {
  2430. free_log_tree(trans, root->log_root);
  2431. root->log_root = NULL;
  2432. }
  2433. return 0;
  2434. }
  2435. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2436. struct btrfs_fs_info *fs_info)
  2437. {
  2438. if (fs_info->log_root_tree) {
  2439. free_log_tree(trans, fs_info->log_root_tree);
  2440. fs_info->log_root_tree = NULL;
  2441. }
  2442. return 0;
  2443. }
  2444. /*
  2445. * If both a file and directory are logged, and unlinks or renames are
  2446. * mixed in, we have a few interesting corners:
  2447. *
  2448. * create file X in dir Y
  2449. * link file X to X.link in dir Y
  2450. * fsync file X
  2451. * unlink file X but leave X.link
  2452. * fsync dir Y
  2453. *
  2454. * After a crash we would expect only X.link to exist. But file X
  2455. * didn't get fsync'd again so the log has back refs for X and X.link.
  2456. *
  2457. * We solve this by removing directory entries and inode backrefs from the
  2458. * log when a file that was logged in the current transaction is
  2459. * unlinked. Any later fsync will include the updated log entries, and
  2460. * we'll be able to reconstruct the proper directory items from backrefs.
  2461. *
  2462. * This optimizations allows us to avoid relogging the entire inode
  2463. * or the entire directory.
  2464. */
  2465. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2466. struct btrfs_root *root,
  2467. const char *name, int name_len,
  2468. struct inode *dir, u64 index)
  2469. {
  2470. struct btrfs_root *log;
  2471. struct btrfs_dir_item *di;
  2472. struct btrfs_path *path;
  2473. int ret;
  2474. int err = 0;
  2475. int bytes_del = 0;
  2476. u64 dir_ino = btrfs_ino(dir);
  2477. if (BTRFS_I(dir)->logged_trans < trans->transid)
  2478. return 0;
  2479. ret = join_running_log_trans(root);
  2480. if (ret)
  2481. return 0;
  2482. mutex_lock(&BTRFS_I(dir)->log_mutex);
  2483. log = root->log_root;
  2484. path = btrfs_alloc_path();
  2485. if (!path) {
  2486. err = -ENOMEM;
  2487. goto out_unlock;
  2488. }
  2489. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2490. name, name_len, -1);
  2491. if (IS_ERR(di)) {
  2492. err = PTR_ERR(di);
  2493. goto fail;
  2494. }
  2495. if (di) {
  2496. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2497. bytes_del += name_len;
  2498. if (ret) {
  2499. err = ret;
  2500. goto fail;
  2501. }
  2502. }
  2503. btrfs_release_path(path);
  2504. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2505. index, name, name_len, -1);
  2506. if (IS_ERR(di)) {
  2507. err = PTR_ERR(di);
  2508. goto fail;
  2509. }
  2510. if (di) {
  2511. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2512. bytes_del += name_len;
  2513. if (ret) {
  2514. err = ret;
  2515. goto fail;
  2516. }
  2517. }
  2518. /* update the directory size in the log to reflect the names
  2519. * we have removed
  2520. */
  2521. if (bytes_del) {
  2522. struct btrfs_key key;
  2523. key.objectid = dir_ino;
  2524. key.offset = 0;
  2525. key.type = BTRFS_INODE_ITEM_KEY;
  2526. btrfs_release_path(path);
  2527. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2528. if (ret < 0) {
  2529. err = ret;
  2530. goto fail;
  2531. }
  2532. if (ret == 0) {
  2533. struct btrfs_inode_item *item;
  2534. u64 i_size;
  2535. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2536. struct btrfs_inode_item);
  2537. i_size = btrfs_inode_size(path->nodes[0], item);
  2538. if (i_size > bytes_del)
  2539. i_size -= bytes_del;
  2540. else
  2541. i_size = 0;
  2542. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2543. btrfs_mark_buffer_dirty(path->nodes[0]);
  2544. } else
  2545. ret = 0;
  2546. btrfs_release_path(path);
  2547. }
  2548. fail:
  2549. btrfs_free_path(path);
  2550. out_unlock:
  2551. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2552. if (ret == -ENOSPC) {
  2553. btrfs_set_log_full_commit(root->fs_info, trans);
  2554. ret = 0;
  2555. } else if (ret < 0)
  2556. btrfs_abort_transaction(trans, root, ret);
  2557. btrfs_end_log_trans(root);
  2558. return err;
  2559. }
  2560. /* see comments for btrfs_del_dir_entries_in_log */
  2561. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2562. struct btrfs_root *root,
  2563. const char *name, int name_len,
  2564. struct inode *inode, u64 dirid)
  2565. {
  2566. struct btrfs_root *log;
  2567. u64 index;
  2568. int ret;
  2569. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2570. return 0;
  2571. ret = join_running_log_trans(root);
  2572. if (ret)
  2573. return 0;
  2574. log = root->log_root;
  2575. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2576. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2577. dirid, &index);
  2578. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2579. if (ret == -ENOSPC) {
  2580. btrfs_set_log_full_commit(root->fs_info, trans);
  2581. ret = 0;
  2582. } else if (ret < 0 && ret != -ENOENT)
  2583. btrfs_abort_transaction(trans, root, ret);
  2584. btrfs_end_log_trans(root);
  2585. return ret;
  2586. }
  2587. /*
  2588. * creates a range item in the log for 'dirid'. first_offset and
  2589. * last_offset tell us which parts of the key space the log should
  2590. * be considered authoritative for.
  2591. */
  2592. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2593. struct btrfs_root *log,
  2594. struct btrfs_path *path,
  2595. int key_type, u64 dirid,
  2596. u64 first_offset, u64 last_offset)
  2597. {
  2598. int ret;
  2599. struct btrfs_key key;
  2600. struct btrfs_dir_log_item *item;
  2601. key.objectid = dirid;
  2602. key.offset = first_offset;
  2603. if (key_type == BTRFS_DIR_ITEM_KEY)
  2604. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2605. else
  2606. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2607. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2608. if (ret)
  2609. return ret;
  2610. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2611. struct btrfs_dir_log_item);
  2612. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2613. btrfs_mark_buffer_dirty(path->nodes[0]);
  2614. btrfs_release_path(path);
  2615. return 0;
  2616. }
  2617. /*
  2618. * log all the items included in the current transaction for a given
  2619. * directory. This also creates the range items in the log tree required
  2620. * to replay anything deleted before the fsync
  2621. */
  2622. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2623. struct btrfs_root *root, struct inode *inode,
  2624. struct btrfs_path *path,
  2625. struct btrfs_path *dst_path, int key_type,
  2626. u64 min_offset, u64 *last_offset_ret)
  2627. {
  2628. struct btrfs_key min_key;
  2629. struct btrfs_root *log = root->log_root;
  2630. struct extent_buffer *src;
  2631. int err = 0;
  2632. int ret;
  2633. int i;
  2634. int nritems;
  2635. u64 first_offset = min_offset;
  2636. u64 last_offset = (u64)-1;
  2637. u64 ino = btrfs_ino(inode);
  2638. log = root->log_root;
  2639. min_key.objectid = ino;
  2640. min_key.type = key_type;
  2641. min_key.offset = min_offset;
  2642. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  2643. /*
  2644. * we didn't find anything from this transaction, see if there
  2645. * is anything at all
  2646. */
  2647. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2648. min_key.objectid = ino;
  2649. min_key.type = key_type;
  2650. min_key.offset = (u64)-1;
  2651. btrfs_release_path(path);
  2652. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2653. if (ret < 0) {
  2654. btrfs_release_path(path);
  2655. return ret;
  2656. }
  2657. ret = btrfs_previous_item(root, path, ino, key_type);
  2658. /* if ret == 0 there are items for this type,
  2659. * create a range to tell us the last key of this type.
  2660. * otherwise, there are no items in this directory after
  2661. * *min_offset, and we create a range to indicate that.
  2662. */
  2663. if (ret == 0) {
  2664. struct btrfs_key tmp;
  2665. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2666. path->slots[0]);
  2667. if (key_type == tmp.type)
  2668. first_offset = max(min_offset, tmp.offset) + 1;
  2669. }
  2670. goto done;
  2671. }
  2672. /* go backward to find any previous key */
  2673. ret = btrfs_previous_item(root, path, ino, key_type);
  2674. if (ret == 0) {
  2675. struct btrfs_key tmp;
  2676. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2677. if (key_type == tmp.type) {
  2678. first_offset = tmp.offset;
  2679. ret = overwrite_item(trans, log, dst_path,
  2680. path->nodes[0], path->slots[0],
  2681. &tmp);
  2682. if (ret) {
  2683. err = ret;
  2684. goto done;
  2685. }
  2686. }
  2687. }
  2688. btrfs_release_path(path);
  2689. /* find the first key from this transaction again */
  2690. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2691. if (WARN_ON(ret != 0))
  2692. goto done;
  2693. /*
  2694. * we have a block from this transaction, log every item in it
  2695. * from our directory
  2696. */
  2697. while (1) {
  2698. struct btrfs_key tmp;
  2699. src = path->nodes[0];
  2700. nritems = btrfs_header_nritems(src);
  2701. for (i = path->slots[0]; i < nritems; i++) {
  2702. btrfs_item_key_to_cpu(src, &min_key, i);
  2703. if (min_key.objectid != ino || min_key.type != key_type)
  2704. goto done;
  2705. ret = overwrite_item(trans, log, dst_path, src, i,
  2706. &min_key);
  2707. if (ret) {
  2708. err = ret;
  2709. goto done;
  2710. }
  2711. }
  2712. path->slots[0] = nritems;
  2713. /*
  2714. * look ahead to the next item and see if it is also
  2715. * from this directory and from this transaction
  2716. */
  2717. ret = btrfs_next_leaf(root, path);
  2718. if (ret == 1) {
  2719. last_offset = (u64)-1;
  2720. goto done;
  2721. }
  2722. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2723. if (tmp.objectid != ino || tmp.type != key_type) {
  2724. last_offset = (u64)-1;
  2725. goto done;
  2726. }
  2727. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2728. ret = overwrite_item(trans, log, dst_path,
  2729. path->nodes[0], path->slots[0],
  2730. &tmp);
  2731. if (ret)
  2732. err = ret;
  2733. else
  2734. last_offset = tmp.offset;
  2735. goto done;
  2736. }
  2737. }
  2738. done:
  2739. btrfs_release_path(path);
  2740. btrfs_release_path(dst_path);
  2741. if (err == 0) {
  2742. *last_offset_ret = last_offset;
  2743. /*
  2744. * insert the log range keys to indicate where the log
  2745. * is valid
  2746. */
  2747. ret = insert_dir_log_key(trans, log, path, key_type,
  2748. ino, first_offset, last_offset);
  2749. if (ret)
  2750. err = ret;
  2751. }
  2752. return err;
  2753. }
  2754. /*
  2755. * logging directories is very similar to logging inodes, We find all the items
  2756. * from the current transaction and write them to the log.
  2757. *
  2758. * The recovery code scans the directory in the subvolume, and if it finds a
  2759. * key in the range logged that is not present in the log tree, then it means
  2760. * that dir entry was unlinked during the transaction.
  2761. *
  2762. * In order for that scan to work, we must include one key smaller than
  2763. * the smallest logged by this transaction and one key larger than the largest
  2764. * key logged by this transaction.
  2765. */
  2766. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2767. struct btrfs_root *root, struct inode *inode,
  2768. struct btrfs_path *path,
  2769. struct btrfs_path *dst_path)
  2770. {
  2771. u64 min_key;
  2772. u64 max_key;
  2773. int ret;
  2774. int key_type = BTRFS_DIR_ITEM_KEY;
  2775. again:
  2776. min_key = 0;
  2777. max_key = 0;
  2778. while (1) {
  2779. ret = log_dir_items(trans, root, inode, path,
  2780. dst_path, key_type, min_key,
  2781. &max_key);
  2782. if (ret)
  2783. return ret;
  2784. if (max_key == (u64)-1)
  2785. break;
  2786. min_key = max_key + 1;
  2787. }
  2788. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2789. key_type = BTRFS_DIR_INDEX_KEY;
  2790. goto again;
  2791. }
  2792. return 0;
  2793. }
  2794. /*
  2795. * a helper function to drop items from the log before we relog an
  2796. * inode. max_key_type indicates the highest item type to remove.
  2797. * This cannot be run for file data extents because it does not
  2798. * free the extents they point to.
  2799. */
  2800. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2801. struct btrfs_root *log,
  2802. struct btrfs_path *path,
  2803. u64 objectid, int max_key_type)
  2804. {
  2805. int ret;
  2806. struct btrfs_key key;
  2807. struct btrfs_key found_key;
  2808. int start_slot;
  2809. key.objectid = objectid;
  2810. key.type = max_key_type;
  2811. key.offset = (u64)-1;
  2812. while (1) {
  2813. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2814. BUG_ON(ret == 0); /* Logic error */
  2815. if (ret < 0)
  2816. break;
  2817. if (path->slots[0] == 0)
  2818. break;
  2819. path->slots[0]--;
  2820. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2821. path->slots[0]);
  2822. if (found_key.objectid != objectid)
  2823. break;
  2824. found_key.offset = 0;
  2825. found_key.type = 0;
  2826. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  2827. &start_slot);
  2828. ret = btrfs_del_items(trans, log, path, start_slot,
  2829. path->slots[0] - start_slot + 1);
  2830. /*
  2831. * If start slot isn't 0 then we don't need to re-search, we've
  2832. * found the last guy with the objectid in this tree.
  2833. */
  2834. if (ret || start_slot != 0)
  2835. break;
  2836. btrfs_release_path(path);
  2837. }
  2838. btrfs_release_path(path);
  2839. if (ret > 0)
  2840. ret = 0;
  2841. return ret;
  2842. }
  2843. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2844. struct extent_buffer *leaf,
  2845. struct btrfs_inode_item *item,
  2846. struct inode *inode, int log_inode_only)
  2847. {
  2848. struct btrfs_map_token token;
  2849. btrfs_init_map_token(&token);
  2850. if (log_inode_only) {
  2851. /* set the generation to zero so the recover code
  2852. * can tell the difference between an logging
  2853. * just to say 'this inode exists' and a logging
  2854. * to say 'update this inode with these values'
  2855. */
  2856. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  2857. btrfs_set_token_inode_size(leaf, item, 0, &token);
  2858. } else {
  2859. btrfs_set_token_inode_generation(leaf, item,
  2860. BTRFS_I(inode)->generation,
  2861. &token);
  2862. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  2863. }
  2864. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  2865. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  2866. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  2867. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  2868. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  2869. inode->i_atime.tv_sec, &token);
  2870. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  2871. inode->i_atime.tv_nsec, &token);
  2872. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  2873. inode->i_mtime.tv_sec, &token);
  2874. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2875. inode->i_mtime.tv_nsec, &token);
  2876. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  2877. inode->i_ctime.tv_sec, &token);
  2878. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2879. inode->i_ctime.tv_nsec, &token);
  2880. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  2881. &token);
  2882. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  2883. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  2884. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  2885. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  2886. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  2887. }
  2888. static int log_inode_item(struct btrfs_trans_handle *trans,
  2889. struct btrfs_root *log, struct btrfs_path *path,
  2890. struct inode *inode)
  2891. {
  2892. struct btrfs_inode_item *inode_item;
  2893. int ret;
  2894. ret = btrfs_insert_empty_item(trans, log, path,
  2895. &BTRFS_I(inode)->location,
  2896. sizeof(*inode_item));
  2897. if (ret && ret != -EEXIST)
  2898. return ret;
  2899. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2900. struct btrfs_inode_item);
  2901. fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
  2902. btrfs_release_path(path);
  2903. return 0;
  2904. }
  2905. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2906. struct inode *inode,
  2907. struct btrfs_path *dst_path,
  2908. struct btrfs_path *src_path, u64 *last_extent,
  2909. int start_slot, int nr, int inode_only)
  2910. {
  2911. unsigned long src_offset;
  2912. unsigned long dst_offset;
  2913. struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
  2914. struct btrfs_file_extent_item *extent;
  2915. struct btrfs_inode_item *inode_item;
  2916. struct extent_buffer *src = src_path->nodes[0];
  2917. struct btrfs_key first_key, last_key, key;
  2918. int ret;
  2919. struct btrfs_key *ins_keys;
  2920. u32 *ins_sizes;
  2921. char *ins_data;
  2922. int i;
  2923. struct list_head ordered_sums;
  2924. int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  2925. bool has_extents = false;
  2926. bool need_find_last_extent = true;
  2927. bool done = false;
  2928. INIT_LIST_HEAD(&ordered_sums);
  2929. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2930. nr * sizeof(u32), GFP_NOFS);
  2931. if (!ins_data)
  2932. return -ENOMEM;
  2933. first_key.objectid = (u64)-1;
  2934. ins_sizes = (u32 *)ins_data;
  2935. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2936. for (i = 0; i < nr; i++) {
  2937. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2938. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2939. }
  2940. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2941. ins_keys, ins_sizes, nr);
  2942. if (ret) {
  2943. kfree(ins_data);
  2944. return ret;
  2945. }
  2946. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  2947. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2948. dst_path->slots[0]);
  2949. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2950. if ((i == (nr - 1)))
  2951. last_key = ins_keys[i];
  2952. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2953. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2954. dst_path->slots[0],
  2955. struct btrfs_inode_item);
  2956. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  2957. inode, inode_only == LOG_INODE_EXISTS);
  2958. } else {
  2959. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2960. src_offset, ins_sizes[i]);
  2961. }
  2962. /*
  2963. * We set need_find_last_extent here in case we know we were
  2964. * processing other items and then walk into the first extent in
  2965. * the inode. If we don't hit an extent then nothing changes,
  2966. * we'll do the last search the next time around.
  2967. */
  2968. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  2969. has_extents = true;
  2970. if (first_key.objectid == (u64)-1)
  2971. first_key = ins_keys[i];
  2972. } else {
  2973. need_find_last_extent = false;
  2974. }
  2975. /* take a reference on file data extents so that truncates
  2976. * or deletes of this inode don't have to relog the inode
  2977. * again
  2978. */
  2979. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  2980. !skip_csum) {
  2981. int found_type;
  2982. extent = btrfs_item_ptr(src, start_slot + i,
  2983. struct btrfs_file_extent_item);
  2984. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  2985. continue;
  2986. found_type = btrfs_file_extent_type(src, extent);
  2987. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2988. u64 ds, dl, cs, cl;
  2989. ds = btrfs_file_extent_disk_bytenr(src,
  2990. extent);
  2991. /* ds == 0 is a hole */
  2992. if (ds == 0)
  2993. continue;
  2994. dl = btrfs_file_extent_disk_num_bytes(src,
  2995. extent);
  2996. cs = btrfs_file_extent_offset(src, extent);
  2997. cl = btrfs_file_extent_num_bytes(src,
  2998. extent);
  2999. if (btrfs_file_extent_compression(src,
  3000. extent)) {
  3001. cs = 0;
  3002. cl = dl;
  3003. }
  3004. ret = btrfs_lookup_csums_range(
  3005. log->fs_info->csum_root,
  3006. ds + cs, ds + cs + cl - 1,
  3007. &ordered_sums, 0);
  3008. if (ret) {
  3009. btrfs_release_path(dst_path);
  3010. kfree(ins_data);
  3011. return ret;
  3012. }
  3013. }
  3014. }
  3015. }
  3016. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3017. btrfs_release_path(dst_path);
  3018. kfree(ins_data);
  3019. /*
  3020. * we have to do this after the loop above to avoid changing the
  3021. * log tree while trying to change the log tree.
  3022. */
  3023. ret = 0;
  3024. while (!list_empty(&ordered_sums)) {
  3025. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3026. struct btrfs_ordered_sum,
  3027. list);
  3028. if (!ret)
  3029. ret = btrfs_csum_file_blocks(trans, log, sums);
  3030. list_del(&sums->list);
  3031. kfree(sums);
  3032. }
  3033. if (!has_extents)
  3034. return ret;
  3035. if (need_find_last_extent && *last_extent == first_key.offset) {
  3036. /*
  3037. * We don't have any leafs between our current one and the one
  3038. * we processed before that can have file extent items for our
  3039. * inode (and have a generation number smaller than our current
  3040. * transaction id).
  3041. */
  3042. need_find_last_extent = false;
  3043. }
  3044. /*
  3045. * Because we use btrfs_search_forward we could skip leaves that were
  3046. * not modified and then assume *last_extent is valid when it really
  3047. * isn't. So back up to the previous leaf and read the end of the last
  3048. * extent before we go and fill in holes.
  3049. */
  3050. if (need_find_last_extent) {
  3051. u64 len;
  3052. ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
  3053. if (ret < 0)
  3054. return ret;
  3055. if (ret)
  3056. goto fill_holes;
  3057. if (src_path->slots[0])
  3058. src_path->slots[0]--;
  3059. src = src_path->nodes[0];
  3060. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3061. if (key.objectid != btrfs_ino(inode) ||
  3062. key.type != BTRFS_EXTENT_DATA_KEY)
  3063. goto fill_holes;
  3064. extent = btrfs_item_ptr(src, src_path->slots[0],
  3065. struct btrfs_file_extent_item);
  3066. if (btrfs_file_extent_type(src, extent) ==
  3067. BTRFS_FILE_EXTENT_INLINE) {
  3068. len = btrfs_file_extent_inline_len(src,
  3069. src_path->slots[0],
  3070. extent);
  3071. *last_extent = ALIGN(key.offset + len,
  3072. log->sectorsize);
  3073. } else {
  3074. len = btrfs_file_extent_num_bytes(src, extent);
  3075. *last_extent = key.offset + len;
  3076. }
  3077. }
  3078. fill_holes:
  3079. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3080. * things could have happened
  3081. *
  3082. * 1) A merge could have happened, so we could currently be on a leaf
  3083. * that holds what we were copying in the first place.
  3084. * 2) A split could have happened, and now not all of the items we want
  3085. * are on the same leaf.
  3086. *
  3087. * So we need to adjust how we search for holes, we need to drop the
  3088. * path and re-search for the first extent key we found, and then walk
  3089. * forward until we hit the last one we copied.
  3090. */
  3091. if (need_find_last_extent) {
  3092. /* btrfs_prev_leaf could return 1 without releasing the path */
  3093. btrfs_release_path(src_path);
  3094. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
  3095. src_path, 0, 0);
  3096. if (ret < 0)
  3097. return ret;
  3098. ASSERT(ret == 0);
  3099. src = src_path->nodes[0];
  3100. i = src_path->slots[0];
  3101. } else {
  3102. i = start_slot;
  3103. }
  3104. /*
  3105. * Ok so here we need to go through and fill in any holes we may have
  3106. * to make sure that holes are punched for those areas in case they had
  3107. * extents previously.
  3108. */
  3109. while (!done) {
  3110. u64 offset, len;
  3111. u64 extent_end;
  3112. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3113. ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
  3114. if (ret < 0)
  3115. return ret;
  3116. ASSERT(ret == 0);
  3117. src = src_path->nodes[0];
  3118. i = 0;
  3119. }
  3120. btrfs_item_key_to_cpu(src, &key, i);
  3121. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3122. done = true;
  3123. if (key.objectid != btrfs_ino(inode) ||
  3124. key.type != BTRFS_EXTENT_DATA_KEY) {
  3125. i++;
  3126. continue;
  3127. }
  3128. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3129. if (btrfs_file_extent_type(src, extent) ==
  3130. BTRFS_FILE_EXTENT_INLINE) {
  3131. len = btrfs_file_extent_inline_len(src, i, extent);
  3132. extent_end = ALIGN(key.offset + len, log->sectorsize);
  3133. } else {
  3134. len = btrfs_file_extent_num_bytes(src, extent);
  3135. extent_end = key.offset + len;
  3136. }
  3137. i++;
  3138. if (*last_extent == key.offset) {
  3139. *last_extent = extent_end;
  3140. continue;
  3141. }
  3142. offset = *last_extent;
  3143. len = key.offset - *last_extent;
  3144. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3145. offset, 0, 0, len, 0, len, 0,
  3146. 0, 0);
  3147. if (ret)
  3148. break;
  3149. *last_extent = extent_end;
  3150. }
  3151. /*
  3152. * Need to let the callers know we dropped the path so they should
  3153. * re-search.
  3154. */
  3155. if (!ret && need_find_last_extent)
  3156. ret = 1;
  3157. return ret;
  3158. }
  3159. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3160. {
  3161. struct extent_map *em1, *em2;
  3162. em1 = list_entry(a, struct extent_map, list);
  3163. em2 = list_entry(b, struct extent_map, list);
  3164. if (em1->start < em2->start)
  3165. return -1;
  3166. else if (em1->start > em2->start)
  3167. return 1;
  3168. return 0;
  3169. }
  3170. static int log_one_extent(struct btrfs_trans_handle *trans,
  3171. struct inode *inode, struct btrfs_root *root,
  3172. struct extent_map *em, struct btrfs_path *path,
  3173. struct list_head *logged_list)
  3174. {
  3175. struct btrfs_root *log = root->log_root;
  3176. struct btrfs_file_extent_item *fi;
  3177. struct extent_buffer *leaf;
  3178. struct btrfs_ordered_extent *ordered;
  3179. struct list_head ordered_sums;
  3180. struct btrfs_map_token token;
  3181. struct btrfs_key key;
  3182. u64 mod_start = em->mod_start;
  3183. u64 mod_len = em->mod_len;
  3184. u64 csum_offset;
  3185. u64 csum_len;
  3186. u64 extent_offset = em->start - em->orig_start;
  3187. u64 block_len;
  3188. int ret;
  3189. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3190. int extent_inserted = 0;
  3191. INIT_LIST_HEAD(&ordered_sums);
  3192. btrfs_init_map_token(&token);
  3193. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  3194. em->start + em->len, NULL, 0, 1,
  3195. sizeof(*fi), &extent_inserted);
  3196. if (ret)
  3197. return ret;
  3198. if (!extent_inserted) {
  3199. key.objectid = btrfs_ino(inode);
  3200. key.type = BTRFS_EXTENT_DATA_KEY;
  3201. key.offset = em->start;
  3202. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3203. sizeof(*fi));
  3204. if (ret)
  3205. return ret;
  3206. }
  3207. leaf = path->nodes[0];
  3208. fi = btrfs_item_ptr(leaf, path->slots[0],
  3209. struct btrfs_file_extent_item);
  3210. btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
  3211. &token);
  3212. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3213. skip_csum = true;
  3214. btrfs_set_token_file_extent_type(leaf, fi,
  3215. BTRFS_FILE_EXTENT_PREALLOC,
  3216. &token);
  3217. } else {
  3218. btrfs_set_token_file_extent_type(leaf, fi,
  3219. BTRFS_FILE_EXTENT_REG,
  3220. &token);
  3221. if (em->block_start == EXTENT_MAP_HOLE)
  3222. skip_csum = true;
  3223. }
  3224. block_len = max(em->block_len, em->orig_block_len);
  3225. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3226. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3227. em->block_start,
  3228. &token);
  3229. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3230. &token);
  3231. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3232. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3233. em->block_start -
  3234. extent_offset, &token);
  3235. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3236. &token);
  3237. } else {
  3238. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3239. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3240. &token);
  3241. }
  3242. btrfs_set_token_file_extent_offset(leaf, fi,
  3243. em->start - em->orig_start,
  3244. &token);
  3245. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3246. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3247. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3248. &token);
  3249. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3250. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3251. btrfs_mark_buffer_dirty(leaf);
  3252. btrfs_release_path(path);
  3253. if (ret) {
  3254. return ret;
  3255. }
  3256. if (skip_csum)
  3257. return 0;
  3258. /*
  3259. * First check and see if our csums are on our outstanding ordered
  3260. * extents.
  3261. */
  3262. list_for_each_entry(ordered, logged_list, log_list) {
  3263. struct btrfs_ordered_sum *sum;
  3264. if (!mod_len)
  3265. break;
  3266. if (ordered->file_offset + ordered->len <= mod_start ||
  3267. mod_start + mod_len <= ordered->file_offset)
  3268. continue;
  3269. /*
  3270. * We are going to copy all the csums on this ordered extent, so
  3271. * go ahead and adjust mod_start and mod_len in case this
  3272. * ordered extent has already been logged.
  3273. */
  3274. if (ordered->file_offset > mod_start) {
  3275. if (ordered->file_offset + ordered->len >=
  3276. mod_start + mod_len)
  3277. mod_len = ordered->file_offset - mod_start;
  3278. /*
  3279. * If we have this case
  3280. *
  3281. * |--------- logged extent ---------|
  3282. * |----- ordered extent ----|
  3283. *
  3284. * Just don't mess with mod_start and mod_len, we'll
  3285. * just end up logging more csums than we need and it
  3286. * will be ok.
  3287. */
  3288. } else {
  3289. if (ordered->file_offset + ordered->len <
  3290. mod_start + mod_len) {
  3291. mod_len = (mod_start + mod_len) -
  3292. (ordered->file_offset + ordered->len);
  3293. mod_start = ordered->file_offset +
  3294. ordered->len;
  3295. } else {
  3296. mod_len = 0;
  3297. }
  3298. }
  3299. /*
  3300. * To keep us from looping for the above case of an ordered
  3301. * extent that falls inside of the logged extent.
  3302. */
  3303. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3304. &ordered->flags))
  3305. continue;
  3306. if (ordered->csum_bytes_left) {
  3307. btrfs_start_ordered_extent(inode, ordered, 0);
  3308. wait_event(ordered->wait,
  3309. ordered->csum_bytes_left == 0);
  3310. }
  3311. list_for_each_entry(sum, &ordered->list, list) {
  3312. ret = btrfs_csum_file_blocks(trans, log, sum);
  3313. if (ret)
  3314. goto unlocked;
  3315. }
  3316. }
  3317. unlocked:
  3318. if (!mod_len || ret)
  3319. return ret;
  3320. if (em->compress_type) {
  3321. csum_offset = 0;
  3322. csum_len = block_len;
  3323. } else {
  3324. csum_offset = mod_start - em->start;
  3325. csum_len = mod_len;
  3326. }
  3327. /* block start is already adjusted for the file extent offset. */
  3328. ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
  3329. em->block_start + csum_offset,
  3330. em->block_start + csum_offset +
  3331. csum_len - 1, &ordered_sums, 0);
  3332. if (ret)
  3333. return ret;
  3334. while (!list_empty(&ordered_sums)) {
  3335. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3336. struct btrfs_ordered_sum,
  3337. list);
  3338. if (!ret)
  3339. ret = btrfs_csum_file_blocks(trans, log, sums);
  3340. list_del(&sums->list);
  3341. kfree(sums);
  3342. }
  3343. return ret;
  3344. }
  3345. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3346. struct btrfs_root *root,
  3347. struct inode *inode,
  3348. struct btrfs_path *path,
  3349. struct list_head *logged_list)
  3350. {
  3351. struct extent_map *em, *n;
  3352. struct list_head extents;
  3353. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3354. u64 test_gen;
  3355. int ret = 0;
  3356. int num = 0;
  3357. INIT_LIST_HEAD(&extents);
  3358. write_lock(&tree->lock);
  3359. test_gen = root->fs_info->last_trans_committed;
  3360. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3361. list_del_init(&em->list);
  3362. /*
  3363. * Just an arbitrary number, this can be really CPU intensive
  3364. * once we start getting a lot of extents, and really once we
  3365. * have a bunch of extents we just want to commit since it will
  3366. * be faster.
  3367. */
  3368. if (++num > 32768) {
  3369. list_del_init(&tree->modified_extents);
  3370. ret = -EFBIG;
  3371. goto process;
  3372. }
  3373. if (em->generation <= test_gen)
  3374. continue;
  3375. /* Need a ref to keep it from getting evicted from cache */
  3376. atomic_inc(&em->refs);
  3377. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3378. list_add_tail(&em->list, &extents);
  3379. num++;
  3380. }
  3381. list_sort(NULL, &extents, extent_cmp);
  3382. process:
  3383. while (!list_empty(&extents)) {
  3384. em = list_entry(extents.next, struct extent_map, list);
  3385. list_del_init(&em->list);
  3386. /*
  3387. * If we had an error we just need to delete everybody from our
  3388. * private list.
  3389. */
  3390. if (ret) {
  3391. clear_em_logging(tree, em);
  3392. free_extent_map(em);
  3393. continue;
  3394. }
  3395. write_unlock(&tree->lock);
  3396. ret = log_one_extent(trans, inode, root, em, path, logged_list);
  3397. write_lock(&tree->lock);
  3398. clear_em_logging(tree, em);
  3399. free_extent_map(em);
  3400. }
  3401. WARN_ON(!list_empty(&extents));
  3402. write_unlock(&tree->lock);
  3403. btrfs_release_path(path);
  3404. return ret;
  3405. }
  3406. /* log a single inode in the tree log.
  3407. * At least one parent directory for this inode must exist in the tree
  3408. * or be logged already.
  3409. *
  3410. * Any items from this inode changed by the current transaction are copied
  3411. * to the log tree. An extra reference is taken on any extents in this
  3412. * file, allowing us to avoid a whole pile of corner cases around logging
  3413. * blocks that have been removed from the tree.
  3414. *
  3415. * See LOG_INODE_ALL and related defines for a description of what inode_only
  3416. * does.
  3417. *
  3418. * This handles both files and directories.
  3419. */
  3420. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  3421. struct btrfs_root *root, struct inode *inode,
  3422. int inode_only,
  3423. const loff_t start,
  3424. const loff_t end)
  3425. {
  3426. struct btrfs_path *path;
  3427. struct btrfs_path *dst_path;
  3428. struct btrfs_key min_key;
  3429. struct btrfs_key max_key;
  3430. struct btrfs_root *log = root->log_root;
  3431. struct extent_buffer *src = NULL;
  3432. LIST_HEAD(logged_list);
  3433. u64 last_extent = 0;
  3434. int err = 0;
  3435. int ret;
  3436. int nritems;
  3437. int ins_start_slot = 0;
  3438. int ins_nr;
  3439. bool fast_search = false;
  3440. u64 ino = btrfs_ino(inode);
  3441. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3442. path = btrfs_alloc_path();
  3443. if (!path)
  3444. return -ENOMEM;
  3445. dst_path = btrfs_alloc_path();
  3446. if (!dst_path) {
  3447. btrfs_free_path(path);
  3448. return -ENOMEM;
  3449. }
  3450. min_key.objectid = ino;
  3451. min_key.type = BTRFS_INODE_ITEM_KEY;
  3452. min_key.offset = 0;
  3453. max_key.objectid = ino;
  3454. /* today the code can only do partial logging of directories */
  3455. if (S_ISDIR(inode->i_mode) ||
  3456. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3457. &BTRFS_I(inode)->runtime_flags) &&
  3458. inode_only == LOG_INODE_EXISTS))
  3459. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3460. else
  3461. max_key.type = (u8)-1;
  3462. max_key.offset = (u64)-1;
  3463. /* Only run delayed items if we are a dir or a new file */
  3464. if (S_ISDIR(inode->i_mode) ||
  3465. BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
  3466. ret = btrfs_commit_inode_delayed_items(trans, inode);
  3467. if (ret) {
  3468. btrfs_free_path(path);
  3469. btrfs_free_path(dst_path);
  3470. return ret;
  3471. }
  3472. }
  3473. mutex_lock(&BTRFS_I(inode)->log_mutex);
  3474. btrfs_get_logged_extents(inode, &logged_list);
  3475. /*
  3476. * a brute force approach to making sure we get the most uptodate
  3477. * copies of everything.
  3478. */
  3479. if (S_ISDIR(inode->i_mode)) {
  3480. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  3481. if (inode_only == LOG_INODE_EXISTS)
  3482. max_key_type = BTRFS_XATTR_ITEM_KEY;
  3483. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  3484. } else {
  3485. if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3486. &BTRFS_I(inode)->runtime_flags)) {
  3487. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3488. &BTRFS_I(inode)->runtime_flags);
  3489. ret = btrfs_truncate_inode_items(trans, log,
  3490. inode, 0, 0);
  3491. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3492. &BTRFS_I(inode)->runtime_flags) ||
  3493. inode_only == LOG_INODE_EXISTS) {
  3494. if (inode_only == LOG_INODE_ALL)
  3495. fast_search = true;
  3496. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3497. ret = drop_objectid_items(trans, log, path, ino,
  3498. max_key.type);
  3499. } else {
  3500. if (inode_only == LOG_INODE_ALL)
  3501. fast_search = true;
  3502. ret = log_inode_item(trans, log, dst_path, inode);
  3503. if (ret) {
  3504. err = ret;
  3505. goto out_unlock;
  3506. }
  3507. goto log_extents;
  3508. }
  3509. }
  3510. if (ret) {
  3511. err = ret;
  3512. goto out_unlock;
  3513. }
  3514. while (1) {
  3515. ins_nr = 0;
  3516. ret = btrfs_search_forward(root, &min_key,
  3517. path, trans->transid);
  3518. if (ret != 0)
  3519. break;
  3520. again:
  3521. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  3522. if (min_key.objectid != ino)
  3523. break;
  3524. if (min_key.type > max_key.type)
  3525. break;
  3526. src = path->nodes[0];
  3527. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  3528. ins_nr++;
  3529. goto next_slot;
  3530. } else if (!ins_nr) {
  3531. ins_start_slot = path->slots[0];
  3532. ins_nr = 1;
  3533. goto next_slot;
  3534. }
  3535. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3536. ins_start_slot, ins_nr, inode_only);
  3537. if (ret < 0) {
  3538. err = ret;
  3539. goto out_unlock;
  3540. } if (ret) {
  3541. ins_nr = 0;
  3542. btrfs_release_path(path);
  3543. continue;
  3544. }
  3545. ins_nr = 1;
  3546. ins_start_slot = path->slots[0];
  3547. next_slot:
  3548. nritems = btrfs_header_nritems(path->nodes[0]);
  3549. path->slots[0]++;
  3550. if (path->slots[0] < nritems) {
  3551. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  3552. path->slots[0]);
  3553. goto again;
  3554. }
  3555. if (ins_nr) {
  3556. ret = copy_items(trans, inode, dst_path, path,
  3557. &last_extent, ins_start_slot,
  3558. ins_nr, inode_only);
  3559. if (ret < 0) {
  3560. err = ret;
  3561. goto out_unlock;
  3562. }
  3563. ret = 0;
  3564. ins_nr = 0;
  3565. }
  3566. btrfs_release_path(path);
  3567. if (min_key.offset < (u64)-1) {
  3568. min_key.offset++;
  3569. } else if (min_key.type < max_key.type) {
  3570. min_key.type++;
  3571. min_key.offset = 0;
  3572. } else {
  3573. break;
  3574. }
  3575. }
  3576. if (ins_nr) {
  3577. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3578. ins_start_slot, ins_nr, inode_only);
  3579. if (ret < 0) {
  3580. err = ret;
  3581. goto out_unlock;
  3582. }
  3583. ret = 0;
  3584. ins_nr = 0;
  3585. }
  3586. log_extents:
  3587. btrfs_release_path(path);
  3588. btrfs_release_path(dst_path);
  3589. if (fast_search) {
  3590. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  3591. &logged_list);
  3592. if (ret) {
  3593. err = ret;
  3594. goto out_unlock;
  3595. }
  3596. } else if (inode_only == LOG_INODE_ALL) {
  3597. struct extent_map *em, *n;
  3598. write_lock(&em_tree->lock);
  3599. /*
  3600. * We can't just remove every em if we're called for a ranged
  3601. * fsync - that is, one that doesn't cover the whole possible
  3602. * file range (0 to LLONG_MAX). This is because we can have
  3603. * em's that fall outside the range we're logging and therefore
  3604. * their ordered operations haven't completed yet
  3605. * (btrfs_finish_ordered_io() not invoked yet). This means we
  3606. * didn't get their respective file extent item in the fs/subvol
  3607. * tree yet, and need to let the next fast fsync (one which
  3608. * consults the list of modified extent maps) find the em so
  3609. * that it logs a matching file extent item and waits for the
  3610. * respective ordered operation to complete (if it's still
  3611. * running).
  3612. *
  3613. * Removing every em outside the range we're logging would make
  3614. * the next fast fsync not log their matching file extent items,
  3615. * therefore making us lose data after a log replay.
  3616. */
  3617. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  3618. list) {
  3619. const u64 mod_end = em->mod_start + em->mod_len - 1;
  3620. if (em->mod_start >= start && mod_end <= end)
  3621. list_del_init(&em->list);
  3622. }
  3623. write_unlock(&em_tree->lock);
  3624. }
  3625. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  3626. ret = log_directory_changes(trans, root, inode, path, dst_path);
  3627. if (ret) {
  3628. err = ret;
  3629. goto out_unlock;
  3630. }
  3631. }
  3632. write_lock(&em_tree->lock);
  3633. /*
  3634. * If we're doing a ranged fsync and there are still modified extents
  3635. * in the list, we must run on the next fsync call as it might cover
  3636. * those extents (a full fsync or an fsync for other range).
  3637. */
  3638. if (list_empty(&em_tree->modified_extents)) {
  3639. BTRFS_I(inode)->logged_trans = trans->transid;
  3640. BTRFS_I(inode)->last_log_commit =
  3641. BTRFS_I(inode)->last_sub_trans;
  3642. }
  3643. write_unlock(&em_tree->lock);
  3644. out_unlock:
  3645. if (unlikely(err))
  3646. btrfs_put_logged_extents(&logged_list);
  3647. else
  3648. btrfs_submit_logged_extents(&logged_list, log);
  3649. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  3650. btrfs_free_path(path);
  3651. btrfs_free_path(dst_path);
  3652. return err;
  3653. }
  3654. /*
  3655. * follow the dentry parent pointers up the chain and see if any
  3656. * of the directories in it require a full commit before they can
  3657. * be logged. Returns zero if nothing special needs to be done or 1 if
  3658. * a full commit is required.
  3659. */
  3660. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  3661. struct inode *inode,
  3662. struct dentry *parent,
  3663. struct super_block *sb,
  3664. u64 last_committed)
  3665. {
  3666. int ret = 0;
  3667. struct btrfs_root *root;
  3668. struct dentry *old_parent = NULL;
  3669. struct inode *orig_inode = inode;
  3670. /*
  3671. * for regular files, if its inode is already on disk, we don't
  3672. * have to worry about the parents at all. This is because
  3673. * we can use the last_unlink_trans field to record renames
  3674. * and other fun in this file.
  3675. */
  3676. if (S_ISREG(inode->i_mode) &&
  3677. BTRFS_I(inode)->generation <= last_committed &&
  3678. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  3679. goto out;
  3680. if (!S_ISDIR(inode->i_mode)) {
  3681. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3682. goto out;
  3683. inode = parent->d_inode;
  3684. }
  3685. while (1) {
  3686. /*
  3687. * If we are logging a directory then we start with our inode,
  3688. * not our parents inode, so we need to skipp setting the
  3689. * logged_trans so that further down in the log code we don't
  3690. * think this inode has already been logged.
  3691. */
  3692. if (inode != orig_inode)
  3693. BTRFS_I(inode)->logged_trans = trans->transid;
  3694. smp_mb();
  3695. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  3696. root = BTRFS_I(inode)->root;
  3697. /*
  3698. * make sure any commits to the log are forced
  3699. * to be full commits
  3700. */
  3701. btrfs_set_log_full_commit(root->fs_info, trans);
  3702. ret = 1;
  3703. break;
  3704. }
  3705. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3706. break;
  3707. if (IS_ROOT(parent))
  3708. break;
  3709. parent = dget_parent(parent);
  3710. dput(old_parent);
  3711. old_parent = parent;
  3712. inode = parent->d_inode;
  3713. }
  3714. dput(old_parent);
  3715. out:
  3716. return ret;
  3717. }
  3718. /*
  3719. * helper function around btrfs_log_inode to make sure newly created
  3720. * parent directories also end up in the log. A minimal inode and backref
  3721. * only logging is done of any parent directories that are older than
  3722. * the last committed transaction
  3723. */
  3724. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  3725. struct btrfs_root *root, struct inode *inode,
  3726. struct dentry *parent,
  3727. const loff_t start,
  3728. const loff_t end,
  3729. int exists_only,
  3730. struct btrfs_log_ctx *ctx)
  3731. {
  3732. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  3733. struct super_block *sb;
  3734. struct dentry *old_parent = NULL;
  3735. int ret = 0;
  3736. u64 last_committed = root->fs_info->last_trans_committed;
  3737. sb = inode->i_sb;
  3738. if (btrfs_test_opt(root, NOTREELOG)) {
  3739. ret = 1;
  3740. goto end_no_trans;
  3741. }
  3742. /*
  3743. * The prev transaction commit doesn't complete, we need do
  3744. * full commit by ourselves.
  3745. */
  3746. if (root->fs_info->last_trans_log_full_commit >
  3747. root->fs_info->last_trans_committed) {
  3748. ret = 1;
  3749. goto end_no_trans;
  3750. }
  3751. if (root != BTRFS_I(inode)->root ||
  3752. btrfs_root_refs(&root->root_item) == 0) {
  3753. ret = 1;
  3754. goto end_no_trans;
  3755. }
  3756. ret = check_parent_dirs_for_sync(trans, inode, parent,
  3757. sb, last_committed);
  3758. if (ret)
  3759. goto end_no_trans;
  3760. if (btrfs_inode_in_log(inode, trans->transid)) {
  3761. ret = BTRFS_NO_LOG_SYNC;
  3762. goto end_no_trans;
  3763. }
  3764. ret = start_log_trans(trans, root, ctx);
  3765. if (ret)
  3766. goto end_no_trans;
  3767. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end);
  3768. if (ret)
  3769. goto end_trans;
  3770. /*
  3771. * for regular files, if its inode is already on disk, we don't
  3772. * have to worry about the parents at all. This is because
  3773. * we can use the last_unlink_trans field to record renames
  3774. * and other fun in this file.
  3775. */
  3776. if (S_ISREG(inode->i_mode) &&
  3777. BTRFS_I(inode)->generation <= last_committed &&
  3778. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  3779. ret = 0;
  3780. goto end_trans;
  3781. }
  3782. inode_only = LOG_INODE_EXISTS;
  3783. while (1) {
  3784. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3785. break;
  3786. inode = parent->d_inode;
  3787. if (root != BTRFS_I(inode)->root)
  3788. break;
  3789. if (BTRFS_I(inode)->generation >
  3790. root->fs_info->last_trans_committed) {
  3791. ret = btrfs_log_inode(trans, root, inode, inode_only,
  3792. 0, LLONG_MAX);
  3793. if (ret)
  3794. goto end_trans;
  3795. }
  3796. if (IS_ROOT(parent))
  3797. break;
  3798. parent = dget_parent(parent);
  3799. dput(old_parent);
  3800. old_parent = parent;
  3801. }
  3802. ret = 0;
  3803. end_trans:
  3804. dput(old_parent);
  3805. if (ret < 0) {
  3806. btrfs_set_log_full_commit(root->fs_info, trans);
  3807. ret = 1;
  3808. }
  3809. if (ret)
  3810. btrfs_remove_log_ctx(root, ctx);
  3811. btrfs_end_log_trans(root);
  3812. end_no_trans:
  3813. return ret;
  3814. }
  3815. /*
  3816. * it is not safe to log dentry if the chunk root has added new
  3817. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  3818. * If this returns 1, you must commit the transaction to safely get your
  3819. * data on disk.
  3820. */
  3821. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  3822. struct btrfs_root *root, struct dentry *dentry,
  3823. const loff_t start,
  3824. const loff_t end,
  3825. struct btrfs_log_ctx *ctx)
  3826. {
  3827. struct dentry *parent = dget_parent(dentry);
  3828. int ret;
  3829. ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
  3830. start, end, 0, ctx);
  3831. dput(parent);
  3832. return ret;
  3833. }
  3834. /*
  3835. * should be called during mount to recover any replay any log trees
  3836. * from the FS
  3837. */
  3838. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  3839. {
  3840. int ret;
  3841. struct btrfs_path *path;
  3842. struct btrfs_trans_handle *trans;
  3843. struct btrfs_key key;
  3844. struct btrfs_key found_key;
  3845. struct btrfs_key tmp_key;
  3846. struct btrfs_root *log;
  3847. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  3848. struct walk_control wc = {
  3849. .process_func = process_one_buffer,
  3850. .stage = 0,
  3851. };
  3852. path = btrfs_alloc_path();
  3853. if (!path)
  3854. return -ENOMEM;
  3855. fs_info->log_root_recovering = 1;
  3856. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3857. if (IS_ERR(trans)) {
  3858. ret = PTR_ERR(trans);
  3859. goto error;
  3860. }
  3861. wc.trans = trans;
  3862. wc.pin = 1;
  3863. ret = walk_log_tree(trans, log_root_tree, &wc);
  3864. if (ret) {
  3865. btrfs_error(fs_info, ret, "Failed to pin buffers while "
  3866. "recovering log root tree.");
  3867. goto error;
  3868. }
  3869. again:
  3870. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  3871. key.offset = (u64)-1;
  3872. key.type = BTRFS_ROOT_ITEM_KEY;
  3873. while (1) {
  3874. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  3875. if (ret < 0) {
  3876. btrfs_error(fs_info, ret,
  3877. "Couldn't find tree log root.");
  3878. goto error;
  3879. }
  3880. if (ret > 0) {
  3881. if (path->slots[0] == 0)
  3882. break;
  3883. path->slots[0]--;
  3884. }
  3885. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3886. path->slots[0]);
  3887. btrfs_release_path(path);
  3888. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3889. break;
  3890. log = btrfs_read_fs_root(log_root_tree, &found_key);
  3891. if (IS_ERR(log)) {
  3892. ret = PTR_ERR(log);
  3893. btrfs_error(fs_info, ret,
  3894. "Couldn't read tree log root.");
  3895. goto error;
  3896. }
  3897. tmp_key.objectid = found_key.offset;
  3898. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  3899. tmp_key.offset = (u64)-1;
  3900. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  3901. if (IS_ERR(wc.replay_dest)) {
  3902. ret = PTR_ERR(wc.replay_dest);
  3903. free_extent_buffer(log->node);
  3904. free_extent_buffer(log->commit_root);
  3905. kfree(log);
  3906. btrfs_error(fs_info, ret, "Couldn't read target root "
  3907. "for tree log recovery.");
  3908. goto error;
  3909. }
  3910. wc.replay_dest->log_root = log;
  3911. btrfs_record_root_in_trans(trans, wc.replay_dest);
  3912. ret = walk_log_tree(trans, log, &wc);
  3913. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  3914. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  3915. path);
  3916. }
  3917. key.offset = found_key.offset - 1;
  3918. wc.replay_dest->log_root = NULL;
  3919. free_extent_buffer(log->node);
  3920. free_extent_buffer(log->commit_root);
  3921. kfree(log);
  3922. if (ret)
  3923. goto error;
  3924. if (found_key.offset == 0)
  3925. break;
  3926. }
  3927. btrfs_release_path(path);
  3928. /* step one is to pin it all, step two is to replay just inodes */
  3929. if (wc.pin) {
  3930. wc.pin = 0;
  3931. wc.process_func = replay_one_buffer;
  3932. wc.stage = LOG_WALK_REPLAY_INODES;
  3933. goto again;
  3934. }
  3935. /* step three is to replay everything */
  3936. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  3937. wc.stage++;
  3938. goto again;
  3939. }
  3940. btrfs_free_path(path);
  3941. /* step 4: commit the transaction, which also unpins the blocks */
  3942. ret = btrfs_commit_transaction(trans, fs_info->tree_root);
  3943. if (ret)
  3944. return ret;
  3945. free_extent_buffer(log_root_tree->node);
  3946. log_root_tree->log_root = NULL;
  3947. fs_info->log_root_recovering = 0;
  3948. kfree(log_root_tree);
  3949. return 0;
  3950. error:
  3951. if (wc.trans)
  3952. btrfs_end_transaction(wc.trans, fs_info->tree_root);
  3953. btrfs_free_path(path);
  3954. return ret;
  3955. }
  3956. /*
  3957. * there are some corner cases where we want to force a full
  3958. * commit instead of allowing a directory to be logged.
  3959. *
  3960. * They revolve around files there were unlinked from the directory, and
  3961. * this function updates the parent directory so that a full commit is
  3962. * properly done if it is fsync'd later after the unlinks are done.
  3963. */
  3964. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  3965. struct inode *dir, struct inode *inode,
  3966. int for_rename)
  3967. {
  3968. /*
  3969. * when we're logging a file, if it hasn't been renamed
  3970. * or unlinked, and its inode is fully committed on disk,
  3971. * we don't have to worry about walking up the directory chain
  3972. * to log its parents.
  3973. *
  3974. * So, we use the last_unlink_trans field to put this transid
  3975. * into the file. When the file is logged we check it and
  3976. * don't log the parents if the file is fully on disk.
  3977. */
  3978. if (S_ISREG(inode->i_mode))
  3979. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  3980. /*
  3981. * if this directory was already logged any new
  3982. * names for this file/dir will get recorded
  3983. */
  3984. smp_mb();
  3985. if (BTRFS_I(dir)->logged_trans == trans->transid)
  3986. return;
  3987. /*
  3988. * if the inode we're about to unlink was logged,
  3989. * the log will be properly updated for any new names
  3990. */
  3991. if (BTRFS_I(inode)->logged_trans == trans->transid)
  3992. return;
  3993. /*
  3994. * when renaming files across directories, if the directory
  3995. * there we're unlinking from gets fsync'd later on, there's
  3996. * no way to find the destination directory later and fsync it
  3997. * properly. So, we have to be conservative and force commits
  3998. * so the new name gets discovered.
  3999. */
  4000. if (for_rename)
  4001. goto record;
  4002. /* we can safely do the unlink without any special recording */
  4003. return;
  4004. record:
  4005. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  4006. }
  4007. /*
  4008. * Call this after adding a new name for a file and it will properly
  4009. * update the log to reflect the new name.
  4010. *
  4011. * It will return zero if all goes well, and it will return 1 if a
  4012. * full transaction commit is required.
  4013. */
  4014. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  4015. struct inode *inode, struct inode *old_dir,
  4016. struct dentry *parent)
  4017. {
  4018. struct btrfs_root * root = BTRFS_I(inode)->root;
  4019. /*
  4020. * this will force the logging code to walk the dentry chain
  4021. * up for the file
  4022. */
  4023. if (S_ISREG(inode->i_mode))
  4024. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  4025. /*
  4026. * if this inode hasn't been logged and directory we're renaming it
  4027. * from hasn't been logged, we don't need to log it
  4028. */
  4029. if (BTRFS_I(inode)->logged_trans <=
  4030. root->fs_info->last_trans_committed &&
  4031. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  4032. root->fs_info->last_trans_committed))
  4033. return 0;
  4034. return btrfs_log_inode_parent(trans, root, inode, parent, 0,
  4035. LLONG_MAX, 1, NULL);
  4036. }