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