tree-log.c 79 KB

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