tree-log.c 76 KB

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