tree-log.c 76 KB

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