tree-log.c 85 KB

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