tree-log.c 107 KB

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