tree-log.c 85 KB

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