tree-log.c 100 KB

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