tree-log.c 85 KB

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