tree-log.c 109 KB

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