tree-log.c 84 KB

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