tree-log.c 119 KB

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