tree-log.c 108 KB

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