tree-log.c 94 KB

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