tree-log.c 105 KB

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