tree-log.c 109 KB

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