tree-log.c 109 KB

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