tree-log.c 87 KB

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