tree-log.c 116 KB

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