tree-log.c 109 KB

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