tree-log.c 92 KB

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