tree-log.c 92 KB

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