tree-log.c 108 KB

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