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