tree-log.c 109 KB

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