tree-log.c 84 KB

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