tree-log.c 108 KB

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