tree-log.c 116 KB

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