tree-log.c 109 KB

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