tree-log.c 121 KB

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