tree-log.c 76 KB

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