tree-log.c 106 KB

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