tree-log.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120
  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(root, path, item_size, 1);
  373. else if (found_size < item_size)
  374. btrfs_extend_item(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 orig_level;
  1913. path = btrfs_alloc_path();
  1914. if (!path)
  1915. return -ENOMEM;
  1916. level = btrfs_header_level(log->node);
  1917. orig_level = level;
  1918. path->nodes[level] = log->node;
  1919. extent_buffer_get(log->node);
  1920. path->slots[level] = 0;
  1921. while (1) {
  1922. wret = walk_down_log_tree(trans, log, path, &level, wc);
  1923. if (wret > 0)
  1924. break;
  1925. if (wret < 0) {
  1926. ret = wret;
  1927. goto out;
  1928. }
  1929. wret = walk_up_log_tree(trans, log, path, &level, wc);
  1930. if (wret > 0)
  1931. break;
  1932. if (wret < 0) {
  1933. ret = wret;
  1934. goto out;
  1935. }
  1936. }
  1937. /* was the root node processed? if not, catch it here */
  1938. if (path->nodes[orig_level]) {
  1939. ret = wc->process_func(log, path->nodes[orig_level], wc,
  1940. btrfs_header_generation(path->nodes[orig_level]));
  1941. if (ret)
  1942. goto out;
  1943. if (wc->free) {
  1944. struct extent_buffer *next;
  1945. next = path->nodes[orig_level];
  1946. btrfs_tree_lock(next);
  1947. btrfs_set_lock_blocking(next);
  1948. clean_tree_block(trans, log, next);
  1949. btrfs_wait_tree_block_writeback(next);
  1950. btrfs_tree_unlock(next);
  1951. WARN_ON(log->root_key.objectid !=
  1952. BTRFS_TREE_LOG_OBJECTID);
  1953. ret = btrfs_free_and_pin_reserved_extent(log, next->start,
  1954. next->len);
  1955. BUG_ON(ret); /* -ENOMEM or logic errors */
  1956. }
  1957. }
  1958. out:
  1959. btrfs_free_path(path);
  1960. return ret;
  1961. }
  1962. /*
  1963. * helper function to update the item for a given subvolumes log root
  1964. * in the tree of log roots
  1965. */
  1966. static int update_log_root(struct btrfs_trans_handle *trans,
  1967. struct btrfs_root *log)
  1968. {
  1969. int ret;
  1970. if (log->log_transid == 1) {
  1971. /* insert root item on the first sync */
  1972. ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
  1973. &log->root_key, &log->root_item);
  1974. } else {
  1975. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  1976. &log->root_key, &log->root_item);
  1977. }
  1978. return ret;
  1979. }
  1980. static int wait_log_commit(struct btrfs_trans_handle *trans,
  1981. struct btrfs_root *root, unsigned long transid)
  1982. {
  1983. DEFINE_WAIT(wait);
  1984. int index = transid % 2;
  1985. /*
  1986. * we only allow two pending log transactions at a time,
  1987. * so we know that if ours is more than 2 older than the
  1988. * current transaction, we're done
  1989. */
  1990. do {
  1991. prepare_to_wait(&root->log_commit_wait[index],
  1992. &wait, TASK_UNINTERRUPTIBLE);
  1993. mutex_unlock(&root->log_mutex);
  1994. if (root->fs_info->last_trans_log_full_commit !=
  1995. trans->transid && root->log_transid < transid + 2 &&
  1996. atomic_read(&root->log_commit[index]))
  1997. schedule();
  1998. finish_wait(&root->log_commit_wait[index], &wait);
  1999. mutex_lock(&root->log_mutex);
  2000. } while (root->fs_info->last_trans_log_full_commit !=
  2001. trans->transid && root->log_transid < transid + 2 &&
  2002. atomic_read(&root->log_commit[index]));
  2003. return 0;
  2004. }
  2005. static void wait_for_writer(struct btrfs_trans_handle *trans,
  2006. struct btrfs_root *root)
  2007. {
  2008. DEFINE_WAIT(wait);
  2009. while (root->fs_info->last_trans_log_full_commit !=
  2010. trans->transid && atomic_read(&root->log_writers)) {
  2011. prepare_to_wait(&root->log_writer_wait,
  2012. &wait, TASK_UNINTERRUPTIBLE);
  2013. mutex_unlock(&root->log_mutex);
  2014. if (root->fs_info->last_trans_log_full_commit !=
  2015. trans->transid && atomic_read(&root->log_writers))
  2016. schedule();
  2017. mutex_lock(&root->log_mutex);
  2018. finish_wait(&root->log_writer_wait, &wait);
  2019. }
  2020. }
  2021. /*
  2022. * btrfs_sync_log does sends a given tree log down to the disk and
  2023. * updates the super blocks to record it. When this call is done,
  2024. * you know that any inodes previously logged are safely on disk only
  2025. * if it returns 0.
  2026. *
  2027. * Any other return value means you need to call btrfs_commit_transaction.
  2028. * Some of the edge cases for fsyncing directories that have had unlinks
  2029. * or renames done in the past mean that sometimes the only safe
  2030. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2031. * that has happened.
  2032. */
  2033. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2034. struct btrfs_root *root)
  2035. {
  2036. int index1;
  2037. int index2;
  2038. int mark;
  2039. int ret;
  2040. struct btrfs_root *log = root->log_root;
  2041. struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
  2042. unsigned long log_transid = 0;
  2043. mutex_lock(&root->log_mutex);
  2044. log_transid = root->log_transid;
  2045. index1 = root->log_transid % 2;
  2046. if (atomic_read(&root->log_commit[index1])) {
  2047. wait_log_commit(trans, root, root->log_transid);
  2048. mutex_unlock(&root->log_mutex);
  2049. return 0;
  2050. }
  2051. atomic_set(&root->log_commit[index1], 1);
  2052. /* wait for previous tree log sync to complete */
  2053. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2054. wait_log_commit(trans, root, root->log_transid - 1);
  2055. while (1) {
  2056. int batch = atomic_read(&root->log_batch);
  2057. /* when we're on an ssd, just kick the log commit out */
  2058. if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
  2059. mutex_unlock(&root->log_mutex);
  2060. schedule_timeout_uninterruptible(1);
  2061. mutex_lock(&root->log_mutex);
  2062. }
  2063. wait_for_writer(trans, root);
  2064. if (batch == atomic_read(&root->log_batch))
  2065. break;
  2066. }
  2067. /* bail out if we need to do a full commit */
  2068. if (root->fs_info->last_trans_log_full_commit == trans->transid) {
  2069. ret = -EAGAIN;
  2070. btrfs_free_logged_extents(log, log_transid);
  2071. mutex_unlock(&root->log_mutex);
  2072. goto out;
  2073. }
  2074. if (log_transid % 2 == 0)
  2075. mark = EXTENT_DIRTY;
  2076. else
  2077. mark = EXTENT_NEW;
  2078. /* we start IO on all the marked extents here, but we don't actually
  2079. * wait for them until later.
  2080. */
  2081. ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
  2082. if (ret) {
  2083. btrfs_abort_transaction(trans, root, ret);
  2084. btrfs_free_logged_extents(log, log_transid);
  2085. mutex_unlock(&root->log_mutex);
  2086. goto out;
  2087. }
  2088. btrfs_set_root_node(&log->root_item, log->node);
  2089. root->log_transid++;
  2090. log->log_transid = root->log_transid;
  2091. root->log_start_pid = 0;
  2092. smp_mb();
  2093. /*
  2094. * IO has been started, blocks of the log tree have WRITTEN flag set
  2095. * in their headers. new modifications of the log will be written to
  2096. * new positions. so it's safe to allow log writers to go in.
  2097. */
  2098. mutex_unlock(&root->log_mutex);
  2099. mutex_lock(&log_root_tree->log_mutex);
  2100. atomic_inc(&log_root_tree->log_batch);
  2101. atomic_inc(&log_root_tree->log_writers);
  2102. mutex_unlock(&log_root_tree->log_mutex);
  2103. ret = update_log_root(trans, log);
  2104. mutex_lock(&log_root_tree->log_mutex);
  2105. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2106. smp_mb();
  2107. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2108. wake_up(&log_root_tree->log_writer_wait);
  2109. }
  2110. if (ret) {
  2111. if (ret != -ENOSPC) {
  2112. btrfs_abort_transaction(trans, root, ret);
  2113. mutex_unlock(&log_root_tree->log_mutex);
  2114. goto out;
  2115. }
  2116. root->fs_info->last_trans_log_full_commit = trans->transid;
  2117. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2118. btrfs_free_logged_extents(log, log_transid);
  2119. mutex_unlock(&log_root_tree->log_mutex);
  2120. ret = -EAGAIN;
  2121. goto out;
  2122. }
  2123. index2 = log_root_tree->log_transid % 2;
  2124. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2125. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2126. wait_log_commit(trans, log_root_tree,
  2127. log_root_tree->log_transid);
  2128. btrfs_free_logged_extents(log, log_transid);
  2129. mutex_unlock(&log_root_tree->log_mutex);
  2130. ret = 0;
  2131. goto out;
  2132. }
  2133. atomic_set(&log_root_tree->log_commit[index2], 1);
  2134. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2135. wait_log_commit(trans, log_root_tree,
  2136. log_root_tree->log_transid - 1);
  2137. }
  2138. wait_for_writer(trans, log_root_tree);
  2139. /*
  2140. * now that we've moved on to the tree of log tree roots,
  2141. * check the full commit flag again
  2142. */
  2143. if (root->fs_info->last_trans_log_full_commit == trans->transid) {
  2144. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2145. btrfs_free_logged_extents(log, log_transid);
  2146. mutex_unlock(&log_root_tree->log_mutex);
  2147. ret = -EAGAIN;
  2148. goto out_wake_log_root;
  2149. }
  2150. ret = btrfs_write_and_wait_marked_extents(log_root_tree,
  2151. &log_root_tree->dirty_log_pages,
  2152. EXTENT_DIRTY | EXTENT_NEW);
  2153. if (ret) {
  2154. btrfs_abort_transaction(trans, root, ret);
  2155. btrfs_free_logged_extents(log, log_transid);
  2156. mutex_unlock(&log_root_tree->log_mutex);
  2157. goto out_wake_log_root;
  2158. }
  2159. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2160. btrfs_wait_logged_extents(log, log_transid);
  2161. btrfs_set_super_log_root(root->fs_info->super_for_commit,
  2162. log_root_tree->node->start);
  2163. btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
  2164. btrfs_header_level(log_root_tree->node));
  2165. log_root_tree->log_transid++;
  2166. smp_mb();
  2167. mutex_unlock(&log_root_tree->log_mutex);
  2168. /*
  2169. * nobody else is going to jump in and write the the ctree
  2170. * super here because the log_commit atomic below is protecting
  2171. * us. We must be called with a transaction handle pinning
  2172. * the running transaction open, so a full commit can't hop
  2173. * in and cause problems either.
  2174. */
  2175. btrfs_scrub_pause_super(root);
  2176. ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
  2177. btrfs_scrub_continue_super(root);
  2178. if (ret) {
  2179. btrfs_abort_transaction(trans, root, ret);
  2180. goto out_wake_log_root;
  2181. }
  2182. mutex_lock(&root->log_mutex);
  2183. if (root->last_log_commit < log_transid)
  2184. root->last_log_commit = log_transid;
  2185. mutex_unlock(&root->log_mutex);
  2186. out_wake_log_root:
  2187. atomic_set(&log_root_tree->log_commit[index2], 0);
  2188. smp_mb();
  2189. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2190. wake_up(&log_root_tree->log_commit_wait[index2]);
  2191. out:
  2192. atomic_set(&root->log_commit[index1], 0);
  2193. smp_mb();
  2194. if (waitqueue_active(&root->log_commit_wait[index1]))
  2195. wake_up(&root->log_commit_wait[index1]);
  2196. return ret;
  2197. }
  2198. static void free_log_tree(struct btrfs_trans_handle *trans,
  2199. struct btrfs_root *log)
  2200. {
  2201. int ret;
  2202. u64 start;
  2203. u64 end;
  2204. struct walk_control wc = {
  2205. .free = 1,
  2206. .process_func = process_one_buffer
  2207. };
  2208. if (trans) {
  2209. ret = walk_log_tree(trans, log, &wc);
  2210. BUG_ON(ret);
  2211. }
  2212. while (1) {
  2213. ret = find_first_extent_bit(&log->dirty_log_pages,
  2214. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2215. NULL);
  2216. if (ret)
  2217. break;
  2218. clear_extent_bits(&log->dirty_log_pages, start, end,
  2219. EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
  2220. }
  2221. /*
  2222. * We may have short-circuited the log tree with the full commit logic
  2223. * and left ordered extents on our list, so clear these out to keep us
  2224. * from leaking inodes and memory.
  2225. */
  2226. btrfs_free_logged_extents(log, 0);
  2227. btrfs_free_logged_extents(log, 1);
  2228. free_extent_buffer(log->node);
  2229. kfree(log);
  2230. }
  2231. /*
  2232. * free all the extents used by the tree log. This should be called
  2233. * at commit time of the full transaction
  2234. */
  2235. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2236. {
  2237. if (root->log_root) {
  2238. free_log_tree(trans, root->log_root);
  2239. root->log_root = NULL;
  2240. }
  2241. return 0;
  2242. }
  2243. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2244. struct btrfs_fs_info *fs_info)
  2245. {
  2246. if (fs_info->log_root_tree) {
  2247. free_log_tree(trans, fs_info->log_root_tree);
  2248. fs_info->log_root_tree = NULL;
  2249. }
  2250. return 0;
  2251. }
  2252. /*
  2253. * If both a file and directory are logged, and unlinks or renames are
  2254. * mixed in, we have a few interesting corners:
  2255. *
  2256. * create file X in dir Y
  2257. * link file X to X.link in dir Y
  2258. * fsync file X
  2259. * unlink file X but leave X.link
  2260. * fsync dir Y
  2261. *
  2262. * After a crash we would expect only X.link to exist. But file X
  2263. * didn't get fsync'd again so the log has back refs for X and X.link.
  2264. *
  2265. * We solve this by removing directory entries and inode backrefs from the
  2266. * log when a file that was logged in the current transaction is
  2267. * unlinked. Any later fsync will include the updated log entries, and
  2268. * we'll be able to reconstruct the proper directory items from backrefs.
  2269. *
  2270. * This optimizations allows us to avoid relogging the entire inode
  2271. * or the entire directory.
  2272. */
  2273. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2274. struct btrfs_root *root,
  2275. const char *name, int name_len,
  2276. struct inode *dir, u64 index)
  2277. {
  2278. struct btrfs_root *log;
  2279. struct btrfs_dir_item *di;
  2280. struct btrfs_path *path;
  2281. int ret;
  2282. int err = 0;
  2283. int bytes_del = 0;
  2284. u64 dir_ino = btrfs_ino(dir);
  2285. if (BTRFS_I(dir)->logged_trans < trans->transid)
  2286. return 0;
  2287. ret = join_running_log_trans(root);
  2288. if (ret)
  2289. return 0;
  2290. mutex_lock(&BTRFS_I(dir)->log_mutex);
  2291. log = root->log_root;
  2292. path = btrfs_alloc_path();
  2293. if (!path) {
  2294. err = -ENOMEM;
  2295. goto out_unlock;
  2296. }
  2297. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2298. name, name_len, -1);
  2299. if (IS_ERR(di)) {
  2300. err = PTR_ERR(di);
  2301. goto fail;
  2302. }
  2303. if (di) {
  2304. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2305. bytes_del += name_len;
  2306. BUG_ON(ret);
  2307. }
  2308. btrfs_release_path(path);
  2309. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2310. index, name, name_len, -1);
  2311. if (IS_ERR(di)) {
  2312. err = PTR_ERR(di);
  2313. goto fail;
  2314. }
  2315. if (di) {
  2316. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2317. bytes_del += name_len;
  2318. BUG_ON(ret);
  2319. }
  2320. /* update the directory size in the log to reflect the names
  2321. * we have removed
  2322. */
  2323. if (bytes_del) {
  2324. struct btrfs_key key;
  2325. key.objectid = dir_ino;
  2326. key.offset = 0;
  2327. key.type = BTRFS_INODE_ITEM_KEY;
  2328. btrfs_release_path(path);
  2329. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2330. if (ret < 0) {
  2331. err = ret;
  2332. goto fail;
  2333. }
  2334. if (ret == 0) {
  2335. struct btrfs_inode_item *item;
  2336. u64 i_size;
  2337. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2338. struct btrfs_inode_item);
  2339. i_size = btrfs_inode_size(path->nodes[0], item);
  2340. if (i_size > bytes_del)
  2341. i_size -= bytes_del;
  2342. else
  2343. i_size = 0;
  2344. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2345. btrfs_mark_buffer_dirty(path->nodes[0]);
  2346. } else
  2347. ret = 0;
  2348. btrfs_release_path(path);
  2349. }
  2350. fail:
  2351. btrfs_free_path(path);
  2352. out_unlock:
  2353. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2354. if (ret == -ENOSPC) {
  2355. root->fs_info->last_trans_log_full_commit = trans->transid;
  2356. ret = 0;
  2357. } else if (ret < 0)
  2358. btrfs_abort_transaction(trans, root, ret);
  2359. btrfs_end_log_trans(root);
  2360. return err;
  2361. }
  2362. /* see comments for btrfs_del_dir_entries_in_log */
  2363. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2364. struct btrfs_root *root,
  2365. const char *name, int name_len,
  2366. struct inode *inode, u64 dirid)
  2367. {
  2368. struct btrfs_root *log;
  2369. u64 index;
  2370. int ret;
  2371. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2372. return 0;
  2373. ret = join_running_log_trans(root);
  2374. if (ret)
  2375. return 0;
  2376. log = root->log_root;
  2377. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2378. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2379. dirid, &index);
  2380. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2381. if (ret == -ENOSPC) {
  2382. root->fs_info->last_trans_log_full_commit = trans->transid;
  2383. ret = 0;
  2384. } else if (ret < 0 && ret != -ENOENT)
  2385. btrfs_abort_transaction(trans, root, ret);
  2386. btrfs_end_log_trans(root);
  2387. return ret;
  2388. }
  2389. /*
  2390. * creates a range item in the log for 'dirid'. first_offset and
  2391. * last_offset tell us which parts of the key space the log should
  2392. * be considered authoritative for.
  2393. */
  2394. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2395. struct btrfs_root *log,
  2396. struct btrfs_path *path,
  2397. int key_type, u64 dirid,
  2398. u64 first_offset, u64 last_offset)
  2399. {
  2400. int ret;
  2401. struct btrfs_key key;
  2402. struct btrfs_dir_log_item *item;
  2403. key.objectid = dirid;
  2404. key.offset = first_offset;
  2405. if (key_type == BTRFS_DIR_ITEM_KEY)
  2406. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2407. else
  2408. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2409. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2410. if (ret)
  2411. return ret;
  2412. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2413. struct btrfs_dir_log_item);
  2414. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2415. btrfs_mark_buffer_dirty(path->nodes[0]);
  2416. btrfs_release_path(path);
  2417. return 0;
  2418. }
  2419. /*
  2420. * log all the items included in the current transaction for a given
  2421. * directory. This also creates the range items in the log tree required
  2422. * to replay anything deleted before the fsync
  2423. */
  2424. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2425. struct btrfs_root *root, struct inode *inode,
  2426. struct btrfs_path *path,
  2427. struct btrfs_path *dst_path, int key_type,
  2428. u64 min_offset, u64 *last_offset_ret)
  2429. {
  2430. struct btrfs_key min_key;
  2431. struct btrfs_key max_key;
  2432. struct btrfs_root *log = root->log_root;
  2433. struct extent_buffer *src;
  2434. int err = 0;
  2435. int ret;
  2436. int i;
  2437. int nritems;
  2438. u64 first_offset = min_offset;
  2439. u64 last_offset = (u64)-1;
  2440. u64 ino = btrfs_ino(inode);
  2441. log = root->log_root;
  2442. max_key.objectid = ino;
  2443. max_key.offset = (u64)-1;
  2444. max_key.type = key_type;
  2445. min_key.objectid = ino;
  2446. min_key.type = key_type;
  2447. min_key.offset = min_offset;
  2448. path->keep_locks = 1;
  2449. ret = btrfs_search_forward(root, &min_key, &max_key,
  2450. path, trans->transid);
  2451. /*
  2452. * we didn't find anything from this transaction, see if there
  2453. * is anything at all
  2454. */
  2455. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2456. min_key.objectid = ino;
  2457. min_key.type = key_type;
  2458. min_key.offset = (u64)-1;
  2459. btrfs_release_path(path);
  2460. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2461. if (ret < 0) {
  2462. btrfs_release_path(path);
  2463. return ret;
  2464. }
  2465. ret = btrfs_previous_item(root, path, ino, key_type);
  2466. /* if ret == 0 there are items for this type,
  2467. * create a range to tell us the last key of this type.
  2468. * otherwise, there are no items in this directory after
  2469. * *min_offset, and we create a range to indicate that.
  2470. */
  2471. if (ret == 0) {
  2472. struct btrfs_key tmp;
  2473. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2474. path->slots[0]);
  2475. if (key_type == tmp.type)
  2476. first_offset = max(min_offset, tmp.offset) + 1;
  2477. }
  2478. goto done;
  2479. }
  2480. /* go backward to find any previous key */
  2481. ret = btrfs_previous_item(root, path, ino, key_type);
  2482. if (ret == 0) {
  2483. struct btrfs_key tmp;
  2484. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2485. if (key_type == tmp.type) {
  2486. first_offset = tmp.offset;
  2487. ret = overwrite_item(trans, log, dst_path,
  2488. path->nodes[0], path->slots[0],
  2489. &tmp);
  2490. if (ret) {
  2491. err = ret;
  2492. goto done;
  2493. }
  2494. }
  2495. }
  2496. btrfs_release_path(path);
  2497. /* find the first key from this transaction again */
  2498. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2499. if (ret != 0) {
  2500. WARN_ON(1);
  2501. goto done;
  2502. }
  2503. /*
  2504. * we have a block from this transaction, log every item in it
  2505. * from our directory
  2506. */
  2507. while (1) {
  2508. struct btrfs_key tmp;
  2509. src = path->nodes[0];
  2510. nritems = btrfs_header_nritems(src);
  2511. for (i = path->slots[0]; i < nritems; i++) {
  2512. btrfs_item_key_to_cpu(src, &min_key, i);
  2513. if (min_key.objectid != ino || min_key.type != key_type)
  2514. goto done;
  2515. ret = overwrite_item(trans, log, dst_path, src, i,
  2516. &min_key);
  2517. if (ret) {
  2518. err = ret;
  2519. goto done;
  2520. }
  2521. }
  2522. path->slots[0] = nritems;
  2523. /*
  2524. * look ahead to the next item and see if it is also
  2525. * from this directory and from this transaction
  2526. */
  2527. ret = btrfs_next_leaf(root, path);
  2528. if (ret == 1) {
  2529. last_offset = (u64)-1;
  2530. goto done;
  2531. }
  2532. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2533. if (tmp.objectid != ino || tmp.type != key_type) {
  2534. last_offset = (u64)-1;
  2535. goto done;
  2536. }
  2537. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2538. ret = overwrite_item(trans, log, dst_path,
  2539. path->nodes[0], path->slots[0],
  2540. &tmp);
  2541. if (ret)
  2542. err = ret;
  2543. else
  2544. last_offset = tmp.offset;
  2545. goto done;
  2546. }
  2547. }
  2548. done:
  2549. btrfs_release_path(path);
  2550. btrfs_release_path(dst_path);
  2551. if (err == 0) {
  2552. *last_offset_ret = last_offset;
  2553. /*
  2554. * insert the log range keys to indicate where the log
  2555. * is valid
  2556. */
  2557. ret = insert_dir_log_key(trans, log, path, key_type,
  2558. ino, first_offset, last_offset);
  2559. if (ret)
  2560. err = ret;
  2561. }
  2562. return err;
  2563. }
  2564. /*
  2565. * logging directories is very similar to logging inodes, We find all the items
  2566. * from the current transaction and write them to the log.
  2567. *
  2568. * The recovery code scans the directory in the subvolume, and if it finds a
  2569. * key in the range logged that is not present in the log tree, then it means
  2570. * that dir entry was unlinked during the transaction.
  2571. *
  2572. * In order for that scan to work, we must include one key smaller than
  2573. * the smallest logged by this transaction and one key larger than the largest
  2574. * key logged by this transaction.
  2575. */
  2576. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2577. struct btrfs_root *root, struct inode *inode,
  2578. struct btrfs_path *path,
  2579. struct btrfs_path *dst_path)
  2580. {
  2581. u64 min_key;
  2582. u64 max_key;
  2583. int ret;
  2584. int key_type = BTRFS_DIR_ITEM_KEY;
  2585. again:
  2586. min_key = 0;
  2587. max_key = 0;
  2588. while (1) {
  2589. ret = log_dir_items(trans, root, inode, path,
  2590. dst_path, key_type, min_key,
  2591. &max_key);
  2592. if (ret)
  2593. return ret;
  2594. if (max_key == (u64)-1)
  2595. break;
  2596. min_key = max_key + 1;
  2597. }
  2598. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2599. key_type = BTRFS_DIR_INDEX_KEY;
  2600. goto again;
  2601. }
  2602. return 0;
  2603. }
  2604. /*
  2605. * a helper function to drop items from the log before we relog an
  2606. * inode. max_key_type indicates the highest item type to remove.
  2607. * This cannot be run for file data extents because it does not
  2608. * free the extents they point to.
  2609. */
  2610. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2611. struct btrfs_root *log,
  2612. struct btrfs_path *path,
  2613. u64 objectid, int max_key_type)
  2614. {
  2615. int ret;
  2616. struct btrfs_key key;
  2617. struct btrfs_key found_key;
  2618. int start_slot;
  2619. key.objectid = objectid;
  2620. key.type = max_key_type;
  2621. key.offset = (u64)-1;
  2622. while (1) {
  2623. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2624. BUG_ON(ret == 0);
  2625. if (ret < 0)
  2626. break;
  2627. if (path->slots[0] == 0)
  2628. break;
  2629. path->slots[0]--;
  2630. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2631. path->slots[0]);
  2632. if (found_key.objectid != objectid)
  2633. break;
  2634. found_key.offset = 0;
  2635. found_key.type = 0;
  2636. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  2637. &start_slot);
  2638. ret = btrfs_del_items(trans, log, path, start_slot,
  2639. path->slots[0] - start_slot + 1);
  2640. /*
  2641. * If start slot isn't 0 then we don't need to re-search, we've
  2642. * found the last guy with the objectid in this tree.
  2643. */
  2644. if (ret || start_slot != 0)
  2645. break;
  2646. btrfs_release_path(path);
  2647. }
  2648. btrfs_release_path(path);
  2649. if (ret > 0)
  2650. ret = 0;
  2651. return ret;
  2652. }
  2653. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2654. struct extent_buffer *leaf,
  2655. struct btrfs_inode_item *item,
  2656. struct inode *inode, int log_inode_only)
  2657. {
  2658. struct btrfs_map_token token;
  2659. btrfs_init_map_token(&token);
  2660. if (log_inode_only) {
  2661. /* set the generation to zero so the recover code
  2662. * can tell the difference between an logging
  2663. * just to say 'this inode exists' and a logging
  2664. * to say 'update this inode with these values'
  2665. */
  2666. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  2667. btrfs_set_token_inode_size(leaf, item, 0, &token);
  2668. } else {
  2669. btrfs_set_token_inode_generation(leaf, item,
  2670. BTRFS_I(inode)->generation,
  2671. &token);
  2672. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  2673. }
  2674. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  2675. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  2676. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  2677. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  2678. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  2679. inode->i_atime.tv_sec, &token);
  2680. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  2681. inode->i_atime.tv_nsec, &token);
  2682. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  2683. inode->i_mtime.tv_sec, &token);
  2684. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2685. inode->i_mtime.tv_nsec, &token);
  2686. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  2687. inode->i_ctime.tv_sec, &token);
  2688. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2689. inode->i_ctime.tv_nsec, &token);
  2690. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  2691. &token);
  2692. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  2693. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  2694. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  2695. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  2696. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  2697. }
  2698. static int log_inode_item(struct btrfs_trans_handle *trans,
  2699. struct btrfs_root *log, struct btrfs_path *path,
  2700. struct inode *inode)
  2701. {
  2702. struct btrfs_inode_item *inode_item;
  2703. struct btrfs_key key;
  2704. int ret;
  2705. memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
  2706. ret = btrfs_insert_empty_item(trans, log, path, &key,
  2707. sizeof(*inode_item));
  2708. if (ret && ret != -EEXIST)
  2709. return ret;
  2710. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2711. struct btrfs_inode_item);
  2712. fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
  2713. btrfs_release_path(path);
  2714. return 0;
  2715. }
  2716. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2717. struct inode *inode,
  2718. struct btrfs_path *dst_path,
  2719. struct extent_buffer *src,
  2720. int start_slot, int nr, int inode_only)
  2721. {
  2722. unsigned long src_offset;
  2723. unsigned long dst_offset;
  2724. struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
  2725. struct btrfs_file_extent_item *extent;
  2726. struct btrfs_inode_item *inode_item;
  2727. int ret;
  2728. struct btrfs_key *ins_keys;
  2729. u32 *ins_sizes;
  2730. char *ins_data;
  2731. int i;
  2732. struct list_head ordered_sums;
  2733. int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  2734. INIT_LIST_HEAD(&ordered_sums);
  2735. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2736. nr * sizeof(u32), GFP_NOFS);
  2737. if (!ins_data)
  2738. return -ENOMEM;
  2739. ins_sizes = (u32 *)ins_data;
  2740. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2741. for (i = 0; i < nr; i++) {
  2742. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2743. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2744. }
  2745. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2746. ins_keys, ins_sizes, nr);
  2747. if (ret) {
  2748. kfree(ins_data);
  2749. return ret;
  2750. }
  2751. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  2752. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2753. dst_path->slots[0]);
  2754. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2755. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2756. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2757. dst_path->slots[0],
  2758. struct btrfs_inode_item);
  2759. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  2760. inode, inode_only == LOG_INODE_EXISTS);
  2761. } else {
  2762. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2763. src_offset, ins_sizes[i]);
  2764. }
  2765. /* take a reference on file data extents so that truncates
  2766. * or deletes of this inode don't have to relog the inode
  2767. * again
  2768. */
  2769. if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
  2770. !skip_csum) {
  2771. int found_type;
  2772. extent = btrfs_item_ptr(src, start_slot + i,
  2773. struct btrfs_file_extent_item);
  2774. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  2775. continue;
  2776. found_type = btrfs_file_extent_type(src, extent);
  2777. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2778. u64 ds, dl, cs, cl;
  2779. ds = btrfs_file_extent_disk_bytenr(src,
  2780. extent);
  2781. /* ds == 0 is a hole */
  2782. if (ds == 0)
  2783. continue;
  2784. dl = btrfs_file_extent_disk_num_bytes(src,
  2785. extent);
  2786. cs = btrfs_file_extent_offset(src, extent);
  2787. cl = btrfs_file_extent_num_bytes(src,
  2788. extent);
  2789. if (btrfs_file_extent_compression(src,
  2790. extent)) {
  2791. cs = 0;
  2792. cl = dl;
  2793. }
  2794. ret = btrfs_lookup_csums_range(
  2795. log->fs_info->csum_root,
  2796. ds + cs, ds + cs + cl - 1,
  2797. &ordered_sums, 0);
  2798. BUG_ON(ret);
  2799. }
  2800. }
  2801. }
  2802. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  2803. btrfs_release_path(dst_path);
  2804. kfree(ins_data);
  2805. /*
  2806. * we have to do this after the loop above to avoid changing the
  2807. * log tree while trying to change the log tree.
  2808. */
  2809. ret = 0;
  2810. while (!list_empty(&ordered_sums)) {
  2811. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  2812. struct btrfs_ordered_sum,
  2813. list);
  2814. if (!ret)
  2815. ret = btrfs_csum_file_blocks(trans, log, sums);
  2816. list_del(&sums->list);
  2817. kfree(sums);
  2818. }
  2819. return ret;
  2820. }
  2821. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  2822. {
  2823. struct extent_map *em1, *em2;
  2824. em1 = list_entry(a, struct extent_map, list);
  2825. em2 = list_entry(b, struct extent_map, list);
  2826. if (em1->start < em2->start)
  2827. return -1;
  2828. else if (em1->start > em2->start)
  2829. return 1;
  2830. return 0;
  2831. }
  2832. static int log_one_extent(struct btrfs_trans_handle *trans,
  2833. struct inode *inode, struct btrfs_root *root,
  2834. struct extent_map *em, struct btrfs_path *path)
  2835. {
  2836. struct btrfs_root *log = root->log_root;
  2837. struct btrfs_file_extent_item *fi;
  2838. struct extent_buffer *leaf;
  2839. struct btrfs_ordered_extent *ordered;
  2840. struct list_head ordered_sums;
  2841. struct btrfs_map_token token;
  2842. struct btrfs_key key;
  2843. u64 mod_start = em->mod_start;
  2844. u64 mod_len = em->mod_len;
  2845. u64 csum_offset;
  2846. u64 csum_len;
  2847. u64 extent_offset = em->start - em->orig_start;
  2848. u64 block_len;
  2849. int ret;
  2850. int index = log->log_transid % 2;
  2851. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  2852. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  2853. em->start + em->len, NULL, 0);
  2854. if (ret)
  2855. return ret;
  2856. INIT_LIST_HEAD(&ordered_sums);
  2857. btrfs_init_map_token(&token);
  2858. key.objectid = btrfs_ino(inode);
  2859. key.type = BTRFS_EXTENT_DATA_KEY;
  2860. key.offset = em->start;
  2861. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
  2862. if (ret)
  2863. return ret;
  2864. leaf = path->nodes[0];
  2865. fi = btrfs_item_ptr(leaf, path->slots[0],
  2866. struct btrfs_file_extent_item);
  2867. btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
  2868. &token);
  2869. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  2870. skip_csum = true;
  2871. btrfs_set_token_file_extent_type(leaf, fi,
  2872. BTRFS_FILE_EXTENT_PREALLOC,
  2873. &token);
  2874. } else {
  2875. btrfs_set_token_file_extent_type(leaf, fi,
  2876. BTRFS_FILE_EXTENT_REG,
  2877. &token);
  2878. if (em->block_start == 0)
  2879. skip_csum = true;
  2880. }
  2881. block_len = max(em->block_len, em->orig_block_len);
  2882. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  2883. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  2884. em->block_start,
  2885. &token);
  2886. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  2887. &token);
  2888. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  2889. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  2890. em->block_start -
  2891. extent_offset, &token);
  2892. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  2893. &token);
  2894. } else {
  2895. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  2896. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  2897. &token);
  2898. }
  2899. btrfs_set_token_file_extent_offset(leaf, fi,
  2900. em->start - em->orig_start,
  2901. &token);
  2902. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  2903. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  2904. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  2905. &token);
  2906. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  2907. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  2908. btrfs_mark_buffer_dirty(leaf);
  2909. btrfs_release_path(path);
  2910. if (ret) {
  2911. return ret;
  2912. }
  2913. if (skip_csum)
  2914. return 0;
  2915. if (em->compress_type) {
  2916. csum_offset = 0;
  2917. csum_len = block_len;
  2918. }
  2919. /*
  2920. * First check and see if our csums are on our outstanding ordered
  2921. * extents.
  2922. */
  2923. again:
  2924. spin_lock_irq(&log->log_extents_lock[index]);
  2925. list_for_each_entry(ordered, &log->logged_list[index], log_list) {
  2926. struct btrfs_ordered_sum *sum;
  2927. if (!mod_len)
  2928. break;
  2929. if (ordered->inode != inode)
  2930. continue;
  2931. if (ordered->file_offset + ordered->len <= mod_start ||
  2932. mod_start + mod_len <= ordered->file_offset)
  2933. continue;
  2934. /*
  2935. * We are going to copy all the csums on this ordered extent, so
  2936. * go ahead and adjust mod_start and mod_len in case this
  2937. * ordered extent has already been logged.
  2938. */
  2939. if (ordered->file_offset > mod_start) {
  2940. if (ordered->file_offset + ordered->len >=
  2941. mod_start + mod_len)
  2942. mod_len = ordered->file_offset - mod_start;
  2943. /*
  2944. * If we have this case
  2945. *
  2946. * |--------- logged extent ---------|
  2947. * |----- ordered extent ----|
  2948. *
  2949. * Just don't mess with mod_start and mod_len, we'll
  2950. * just end up logging more csums than we need and it
  2951. * will be ok.
  2952. */
  2953. } else {
  2954. if (ordered->file_offset + ordered->len <
  2955. mod_start + mod_len) {
  2956. mod_len = (mod_start + mod_len) -
  2957. (ordered->file_offset + ordered->len);
  2958. mod_start = ordered->file_offset +
  2959. ordered->len;
  2960. } else {
  2961. mod_len = 0;
  2962. }
  2963. }
  2964. /*
  2965. * To keep us from looping for the above case of an ordered
  2966. * extent that falls inside of the logged extent.
  2967. */
  2968. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  2969. &ordered->flags))
  2970. continue;
  2971. atomic_inc(&ordered->refs);
  2972. spin_unlock_irq(&log->log_extents_lock[index]);
  2973. /*
  2974. * we've dropped the lock, we must either break or
  2975. * start over after this.
  2976. */
  2977. wait_event(ordered->wait, ordered->csum_bytes_left == 0);
  2978. list_for_each_entry(sum, &ordered->list, list) {
  2979. ret = btrfs_csum_file_blocks(trans, log, sum);
  2980. if (ret) {
  2981. btrfs_put_ordered_extent(ordered);
  2982. goto unlocked;
  2983. }
  2984. }
  2985. btrfs_put_ordered_extent(ordered);
  2986. goto again;
  2987. }
  2988. spin_unlock_irq(&log->log_extents_lock[index]);
  2989. unlocked:
  2990. if (!mod_len || ret)
  2991. return ret;
  2992. csum_offset = mod_start - em->start;
  2993. csum_len = mod_len;
  2994. /* block start is already adjusted for the file extent offset. */
  2995. ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
  2996. em->block_start + csum_offset,
  2997. em->block_start + csum_offset +
  2998. csum_len - 1, &ordered_sums, 0);
  2999. if (ret)
  3000. return ret;
  3001. while (!list_empty(&ordered_sums)) {
  3002. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3003. struct btrfs_ordered_sum,
  3004. list);
  3005. if (!ret)
  3006. ret = btrfs_csum_file_blocks(trans, log, sums);
  3007. list_del(&sums->list);
  3008. kfree(sums);
  3009. }
  3010. return ret;
  3011. }
  3012. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3013. struct btrfs_root *root,
  3014. struct inode *inode,
  3015. struct btrfs_path *path)
  3016. {
  3017. struct extent_map *em, *n;
  3018. struct list_head extents;
  3019. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3020. u64 test_gen;
  3021. int ret = 0;
  3022. int num = 0;
  3023. INIT_LIST_HEAD(&extents);
  3024. write_lock(&tree->lock);
  3025. test_gen = root->fs_info->last_trans_committed;
  3026. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3027. list_del_init(&em->list);
  3028. /*
  3029. * Just an arbitrary number, this can be really CPU intensive
  3030. * once we start getting a lot of extents, and really once we
  3031. * have a bunch of extents we just want to commit since it will
  3032. * be faster.
  3033. */
  3034. if (++num > 32768) {
  3035. list_del_init(&tree->modified_extents);
  3036. ret = -EFBIG;
  3037. goto process;
  3038. }
  3039. if (em->generation <= test_gen)
  3040. continue;
  3041. /* Need a ref to keep it from getting evicted from cache */
  3042. atomic_inc(&em->refs);
  3043. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3044. list_add_tail(&em->list, &extents);
  3045. num++;
  3046. }
  3047. list_sort(NULL, &extents, extent_cmp);
  3048. process:
  3049. while (!list_empty(&extents)) {
  3050. em = list_entry(extents.next, struct extent_map, list);
  3051. list_del_init(&em->list);
  3052. /*
  3053. * If we had an error we just need to delete everybody from our
  3054. * private list.
  3055. */
  3056. if (ret) {
  3057. clear_em_logging(tree, em);
  3058. free_extent_map(em);
  3059. continue;
  3060. }
  3061. write_unlock(&tree->lock);
  3062. ret = log_one_extent(trans, inode, root, em, path);
  3063. write_lock(&tree->lock);
  3064. clear_em_logging(tree, em);
  3065. free_extent_map(em);
  3066. }
  3067. WARN_ON(!list_empty(&extents));
  3068. write_unlock(&tree->lock);
  3069. btrfs_release_path(path);
  3070. return ret;
  3071. }
  3072. /* log a single inode in the tree log.
  3073. * At least one parent directory for this inode must exist in the tree
  3074. * or be logged already.
  3075. *
  3076. * Any items from this inode changed by the current transaction are copied
  3077. * to the log tree. An extra reference is taken on any extents in this
  3078. * file, allowing us to avoid a whole pile of corner cases around logging
  3079. * blocks that have been removed from the tree.
  3080. *
  3081. * See LOG_INODE_ALL and related defines for a description of what inode_only
  3082. * does.
  3083. *
  3084. * This handles both files and directories.
  3085. */
  3086. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  3087. struct btrfs_root *root, struct inode *inode,
  3088. int inode_only)
  3089. {
  3090. struct btrfs_path *path;
  3091. struct btrfs_path *dst_path;
  3092. struct btrfs_key min_key;
  3093. struct btrfs_key max_key;
  3094. struct btrfs_root *log = root->log_root;
  3095. struct extent_buffer *src = NULL;
  3096. int err = 0;
  3097. int ret;
  3098. int nritems;
  3099. int ins_start_slot = 0;
  3100. int ins_nr;
  3101. bool fast_search = false;
  3102. u64 ino = btrfs_ino(inode);
  3103. path = btrfs_alloc_path();
  3104. if (!path)
  3105. return -ENOMEM;
  3106. dst_path = btrfs_alloc_path();
  3107. if (!dst_path) {
  3108. btrfs_free_path(path);
  3109. return -ENOMEM;
  3110. }
  3111. min_key.objectid = ino;
  3112. min_key.type = BTRFS_INODE_ITEM_KEY;
  3113. min_key.offset = 0;
  3114. max_key.objectid = ino;
  3115. /* today the code can only do partial logging of directories */
  3116. if (S_ISDIR(inode->i_mode) ||
  3117. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3118. &BTRFS_I(inode)->runtime_flags) &&
  3119. inode_only == LOG_INODE_EXISTS))
  3120. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3121. else
  3122. max_key.type = (u8)-1;
  3123. max_key.offset = (u64)-1;
  3124. /* Only run delayed items if we are a dir or a new file */
  3125. if (S_ISDIR(inode->i_mode) ||
  3126. BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
  3127. ret = btrfs_commit_inode_delayed_items(trans, inode);
  3128. if (ret) {
  3129. btrfs_free_path(path);
  3130. btrfs_free_path(dst_path);
  3131. return ret;
  3132. }
  3133. }
  3134. mutex_lock(&BTRFS_I(inode)->log_mutex);
  3135. btrfs_get_logged_extents(log, inode);
  3136. /*
  3137. * a brute force approach to making sure we get the most uptodate
  3138. * copies of everything.
  3139. */
  3140. if (S_ISDIR(inode->i_mode)) {
  3141. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  3142. if (inode_only == LOG_INODE_EXISTS)
  3143. max_key_type = BTRFS_XATTR_ITEM_KEY;
  3144. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  3145. } else {
  3146. if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3147. &BTRFS_I(inode)->runtime_flags)) {
  3148. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3149. &BTRFS_I(inode)->runtime_flags);
  3150. ret = btrfs_truncate_inode_items(trans, log,
  3151. inode, 0, 0);
  3152. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3153. &BTRFS_I(inode)->runtime_flags)) {
  3154. if (inode_only == LOG_INODE_ALL)
  3155. fast_search = true;
  3156. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3157. ret = drop_objectid_items(trans, log, path, ino,
  3158. max_key.type);
  3159. } else {
  3160. if (inode_only == LOG_INODE_ALL)
  3161. fast_search = true;
  3162. ret = log_inode_item(trans, log, dst_path, inode);
  3163. if (ret) {
  3164. err = ret;
  3165. goto out_unlock;
  3166. }
  3167. goto log_extents;
  3168. }
  3169. }
  3170. if (ret) {
  3171. err = ret;
  3172. goto out_unlock;
  3173. }
  3174. path->keep_locks = 1;
  3175. while (1) {
  3176. ins_nr = 0;
  3177. ret = btrfs_search_forward(root, &min_key, &max_key,
  3178. path, trans->transid);
  3179. if (ret != 0)
  3180. break;
  3181. again:
  3182. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  3183. if (min_key.objectid != ino)
  3184. break;
  3185. if (min_key.type > max_key.type)
  3186. break;
  3187. src = path->nodes[0];
  3188. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  3189. ins_nr++;
  3190. goto next_slot;
  3191. } else if (!ins_nr) {
  3192. ins_start_slot = path->slots[0];
  3193. ins_nr = 1;
  3194. goto next_slot;
  3195. }
  3196. ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
  3197. ins_nr, inode_only);
  3198. if (ret) {
  3199. err = ret;
  3200. goto out_unlock;
  3201. }
  3202. ins_nr = 1;
  3203. ins_start_slot = path->slots[0];
  3204. next_slot:
  3205. nritems = btrfs_header_nritems(path->nodes[0]);
  3206. path->slots[0]++;
  3207. if (path->slots[0] < nritems) {
  3208. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  3209. path->slots[0]);
  3210. goto again;
  3211. }
  3212. if (ins_nr) {
  3213. ret = copy_items(trans, inode, dst_path, src,
  3214. ins_start_slot,
  3215. ins_nr, inode_only);
  3216. if (ret) {
  3217. err = ret;
  3218. goto out_unlock;
  3219. }
  3220. ins_nr = 0;
  3221. }
  3222. btrfs_release_path(path);
  3223. if (min_key.offset < (u64)-1)
  3224. min_key.offset++;
  3225. else if (min_key.type < (u8)-1)
  3226. min_key.type++;
  3227. else if (min_key.objectid < (u64)-1)
  3228. min_key.objectid++;
  3229. else
  3230. break;
  3231. }
  3232. if (ins_nr) {
  3233. ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
  3234. ins_nr, inode_only);
  3235. if (ret) {
  3236. err = ret;
  3237. goto out_unlock;
  3238. }
  3239. ins_nr = 0;
  3240. }
  3241. log_extents:
  3242. if (fast_search) {
  3243. btrfs_release_path(dst_path);
  3244. ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
  3245. if (ret) {
  3246. err = ret;
  3247. goto out_unlock;
  3248. }
  3249. } else {
  3250. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3251. struct extent_map *em, *n;
  3252. write_lock(&tree->lock);
  3253. list_for_each_entry_safe(em, n, &tree->modified_extents, list)
  3254. list_del_init(&em->list);
  3255. write_unlock(&tree->lock);
  3256. }
  3257. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  3258. btrfs_release_path(path);
  3259. btrfs_release_path(dst_path);
  3260. ret = log_directory_changes(trans, root, inode, path, dst_path);
  3261. if (ret) {
  3262. err = ret;
  3263. goto out_unlock;
  3264. }
  3265. }
  3266. BTRFS_I(inode)->logged_trans = trans->transid;
  3267. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
  3268. out_unlock:
  3269. if (err)
  3270. btrfs_free_logged_extents(log, log->log_transid);
  3271. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  3272. btrfs_free_path(path);
  3273. btrfs_free_path(dst_path);
  3274. return err;
  3275. }
  3276. /*
  3277. * follow the dentry parent pointers up the chain and see if any
  3278. * of the directories in it require a full commit before they can
  3279. * be logged. Returns zero if nothing special needs to be done or 1 if
  3280. * a full commit is required.
  3281. */
  3282. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  3283. struct inode *inode,
  3284. struct dentry *parent,
  3285. struct super_block *sb,
  3286. u64 last_committed)
  3287. {
  3288. int ret = 0;
  3289. struct btrfs_root *root;
  3290. struct dentry *old_parent = NULL;
  3291. /*
  3292. * for regular files, if its inode is already on disk, we don't
  3293. * have to worry about the parents at all. This is because
  3294. * we can use the last_unlink_trans field to record renames
  3295. * and other fun in this file.
  3296. */
  3297. if (S_ISREG(inode->i_mode) &&
  3298. BTRFS_I(inode)->generation <= last_committed &&
  3299. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  3300. goto out;
  3301. if (!S_ISDIR(inode->i_mode)) {
  3302. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3303. goto out;
  3304. inode = parent->d_inode;
  3305. }
  3306. while (1) {
  3307. BTRFS_I(inode)->logged_trans = trans->transid;
  3308. smp_mb();
  3309. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  3310. root = BTRFS_I(inode)->root;
  3311. /*
  3312. * make sure any commits to the log are forced
  3313. * to be full commits
  3314. */
  3315. root->fs_info->last_trans_log_full_commit =
  3316. trans->transid;
  3317. ret = 1;
  3318. break;
  3319. }
  3320. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3321. break;
  3322. if (IS_ROOT(parent))
  3323. break;
  3324. parent = dget_parent(parent);
  3325. dput(old_parent);
  3326. old_parent = parent;
  3327. inode = parent->d_inode;
  3328. }
  3329. dput(old_parent);
  3330. out:
  3331. return ret;
  3332. }
  3333. /*
  3334. * helper function around btrfs_log_inode to make sure newly created
  3335. * parent directories also end up in the log. A minimal inode and backref
  3336. * only logging is done of any parent directories that are older than
  3337. * the last committed transaction
  3338. */
  3339. int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  3340. struct btrfs_root *root, struct inode *inode,
  3341. struct dentry *parent, int exists_only)
  3342. {
  3343. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  3344. struct super_block *sb;
  3345. struct dentry *old_parent = NULL;
  3346. int ret = 0;
  3347. u64 last_committed = root->fs_info->last_trans_committed;
  3348. sb = inode->i_sb;
  3349. if (btrfs_test_opt(root, NOTREELOG)) {
  3350. ret = 1;
  3351. goto end_no_trans;
  3352. }
  3353. if (root->fs_info->last_trans_log_full_commit >
  3354. root->fs_info->last_trans_committed) {
  3355. ret = 1;
  3356. goto end_no_trans;
  3357. }
  3358. if (root != BTRFS_I(inode)->root ||
  3359. btrfs_root_refs(&root->root_item) == 0) {
  3360. ret = 1;
  3361. goto end_no_trans;
  3362. }
  3363. ret = check_parent_dirs_for_sync(trans, inode, parent,
  3364. sb, last_committed);
  3365. if (ret)
  3366. goto end_no_trans;
  3367. if (btrfs_inode_in_log(inode, trans->transid)) {
  3368. ret = BTRFS_NO_LOG_SYNC;
  3369. goto end_no_trans;
  3370. }
  3371. ret = start_log_trans(trans, root);
  3372. if (ret)
  3373. goto end_trans;
  3374. ret = btrfs_log_inode(trans, root, inode, inode_only);
  3375. if (ret)
  3376. goto end_trans;
  3377. /*
  3378. * for regular files, if its inode is already on disk, we don't
  3379. * have to worry about the parents at all. This is because
  3380. * we can use the last_unlink_trans field to record renames
  3381. * and other fun in this file.
  3382. */
  3383. if (S_ISREG(inode->i_mode) &&
  3384. BTRFS_I(inode)->generation <= last_committed &&
  3385. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  3386. ret = 0;
  3387. goto end_trans;
  3388. }
  3389. inode_only = LOG_INODE_EXISTS;
  3390. while (1) {
  3391. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3392. break;
  3393. inode = parent->d_inode;
  3394. if (root != BTRFS_I(inode)->root)
  3395. break;
  3396. if (BTRFS_I(inode)->generation >
  3397. root->fs_info->last_trans_committed) {
  3398. ret = btrfs_log_inode(trans, root, inode, inode_only);
  3399. if (ret)
  3400. goto end_trans;
  3401. }
  3402. if (IS_ROOT(parent))
  3403. break;
  3404. parent = dget_parent(parent);
  3405. dput(old_parent);
  3406. old_parent = parent;
  3407. }
  3408. ret = 0;
  3409. end_trans:
  3410. dput(old_parent);
  3411. if (ret < 0) {
  3412. root->fs_info->last_trans_log_full_commit = trans->transid;
  3413. ret = 1;
  3414. }
  3415. btrfs_end_log_trans(root);
  3416. end_no_trans:
  3417. return ret;
  3418. }
  3419. /*
  3420. * it is not safe to log dentry if the chunk root has added new
  3421. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  3422. * If this returns 1, you must commit the transaction to safely get your
  3423. * data on disk.
  3424. */
  3425. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  3426. struct btrfs_root *root, struct dentry *dentry)
  3427. {
  3428. struct dentry *parent = dget_parent(dentry);
  3429. int ret;
  3430. ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
  3431. dput(parent);
  3432. return ret;
  3433. }
  3434. /*
  3435. * should be called during mount to recover any replay any log trees
  3436. * from the FS
  3437. */
  3438. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  3439. {
  3440. int ret;
  3441. struct btrfs_path *path;
  3442. struct btrfs_trans_handle *trans;
  3443. struct btrfs_key key;
  3444. struct btrfs_key found_key;
  3445. struct btrfs_key tmp_key;
  3446. struct btrfs_root *log;
  3447. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  3448. struct walk_control wc = {
  3449. .process_func = process_one_buffer,
  3450. .stage = 0,
  3451. };
  3452. path = btrfs_alloc_path();
  3453. if (!path)
  3454. return -ENOMEM;
  3455. fs_info->log_root_recovering = 1;
  3456. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3457. if (IS_ERR(trans)) {
  3458. ret = PTR_ERR(trans);
  3459. goto error;
  3460. }
  3461. wc.trans = trans;
  3462. wc.pin = 1;
  3463. ret = walk_log_tree(trans, log_root_tree, &wc);
  3464. if (ret) {
  3465. btrfs_error(fs_info, ret, "Failed to pin buffers while "
  3466. "recovering log root tree.");
  3467. goto error;
  3468. }
  3469. again:
  3470. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  3471. key.offset = (u64)-1;
  3472. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  3473. while (1) {
  3474. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  3475. if (ret < 0) {
  3476. btrfs_error(fs_info, ret,
  3477. "Couldn't find tree log root.");
  3478. goto error;
  3479. }
  3480. if (ret > 0) {
  3481. if (path->slots[0] == 0)
  3482. break;
  3483. path->slots[0]--;
  3484. }
  3485. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3486. path->slots[0]);
  3487. btrfs_release_path(path);
  3488. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3489. break;
  3490. log = btrfs_read_fs_root_no_radix(log_root_tree,
  3491. &found_key);
  3492. if (IS_ERR(log)) {
  3493. ret = PTR_ERR(log);
  3494. btrfs_error(fs_info, ret,
  3495. "Couldn't read tree log root.");
  3496. goto error;
  3497. }
  3498. tmp_key.objectid = found_key.offset;
  3499. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  3500. tmp_key.offset = (u64)-1;
  3501. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  3502. if (IS_ERR(wc.replay_dest)) {
  3503. ret = PTR_ERR(wc.replay_dest);
  3504. btrfs_error(fs_info, ret, "Couldn't read target root "
  3505. "for tree log recovery.");
  3506. goto error;
  3507. }
  3508. wc.replay_dest->log_root = log;
  3509. btrfs_record_root_in_trans(trans, wc.replay_dest);
  3510. ret = walk_log_tree(trans, log, &wc);
  3511. BUG_ON(ret);
  3512. if (wc.stage == LOG_WALK_REPLAY_ALL) {
  3513. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  3514. path);
  3515. BUG_ON(ret);
  3516. }
  3517. key.offset = found_key.offset - 1;
  3518. wc.replay_dest->log_root = NULL;
  3519. free_extent_buffer(log->node);
  3520. free_extent_buffer(log->commit_root);
  3521. kfree(log);
  3522. if (found_key.offset == 0)
  3523. break;
  3524. }
  3525. btrfs_release_path(path);
  3526. /* step one is to pin it all, step two is to replay just inodes */
  3527. if (wc.pin) {
  3528. wc.pin = 0;
  3529. wc.process_func = replay_one_buffer;
  3530. wc.stage = LOG_WALK_REPLAY_INODES;
  3531. goto again;
  3532. }
  3533. /* step three is to replay everything */
  3534. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  3535. wc.stage++;
  3536. goto again;
  3537. }
  3538. btrfs_free_path(path);
  3539. /* step 4: commit the transaction, which also unpins the blocks */
  3540. ret = btrfs_commit_transaction(trans, fs_info->tree_root);
  3541. if (ret)
  3542. return ret;
  3543. free_extent_buffer(log_root_tree->node);
  3544. log_root_tree->log_root = NULL;
  3545. fs_info->log_root_recovering = 0;
  3546. kfree(log_root_tree);
  3547. return 0;
  3548. error:
  3549. btrfs_free_path(path);
  3550. return ret;
  3551. }
  3552. /*
  3553. * there are some corner cases where we want to force a full
  3554. * commit instead of allowing a directory to be logged.
  3555. *
  3556. * They revolve around files there were unlinked from the directory, and
  3557. * this function updates the parent directory so that a full commit is
  3558. * properly done if it is fsync'd later after the unlinks are done.
  3559. */
  3560. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  3561. struct inode *dir, struct inode *inode,
  3562. int for_rename)
  3563. {
  3564. /*
  3565. * when we're logging a file, if it hasn't been renamed
  3566. * or unlinked, and its inode is fully committed on disk,
  3567. * we don't have to worry about walking up the directory chain
  3568. * to log its parents.
  3569. *
  3570. * So, we use the last_unlink_trans field to put this transid
  3571. * into the file. When the file is logged we check it and
  3572. * don't log the parents if the file is fully on disk.
  3573. */
  3574. if (S_ISREG(inode->i_mode))
  3575. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  3576. /*
  3577. * if this directory was already logged any new
  3578. * names for this file/dir will get recorded
  3579. */
  3580. smp_mb();
  3581. if (BTRFS_I(dir)->logged_trans == trans->transid)
  3582. return;
  3583. /*
  3584. * if the inode we're about to unlink was logged,
  3585. * the log will be properly updated for any new names
  3586. */
  3587. if (BTRFS_I(inode)->logged_trans == trans->transid)
  3588. return;
  3589. /*
  3590. * when renaming files across directories, if the directory
  3591. * there we're unlinking from gets fsync'd later on, there's
  3592. * no way to find the destination directory later and fsync it
  3593. * properly. So, we have to be conservative and force commits
  3594. * so the new name gets discovered.
  3595. */
  3596. if (for_rename)
  3597. goto record;
  3598. /* we can safely do the unlink without any special recording */
  3599. return;
  3600. record:
  3601. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  3602. }
  3603. /*
  3604. * Call this after adding a new name for a file and it will properly
  3605. * update the log to reflect the new name.
  3606. *
  3607. * It will return zero if all goes well, and it will return 1 if a
  3608. * full transaction commit is required.
  3609. */
  3610. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  3611. struct inode *inode, struct inode *old_dir,
  3612. struct dentry *parent)
  3613. {
  3614. struct btrfs_root * root = BTRFS_I(inode)->root;
  3615. /*
  3616. * this will force the logging code to walk the dentry chain
  3617. * up for the file
  3618. */
  3619. if (S_ISREG(inode->i_mode))
  3620. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  3621. /*
  3622. * if this inode hasn't been logged and directory we're renaming it
  3623. * from hasn't been logged, we don't need to log it
  3624. */
  3625. if (BTRFS_I(inode)->logged_trans <=
  3626. root->fs_info->last_trans_committed &&
  3627. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  3628. root->fs_info->last_trans_committed))
  3629. return 0;
  3630. return btrfs_log_inode_parent(trans, root, inode, parent, 1);
  3631. }