tree-log.c 108 KB

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