tree-log.c 87 KB

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