tree-log.c 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836
  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. #include "compression.h"
  29. #include "qgroup.h"
  30. /* magic values for the inode_only field in btrfs_log_inode:
  31. *
  32. * LOG_INODE_ALL means to log everything
  33. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  34. * during log replay
  35. */
  36. #define LOG_INODE_ALL 0
  37. #define LOG_INODE_EXISTS 1
  38. #define LOG_OTHER_INODE 2
  39. /*
  40. * directory trouble cases
  41. *
  42. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  43. * log, we must force a full commit before doing an fsync of the directory
  44. * where the unlink was done.
  45. * ---> record transid of last unlink/rename per directory
  46. *
  47. * mkdir foo/some_dir
  48. * normal commit
  49. * rename foo/some_dir foo2/some_dir
  50. * mkdir foo/some_dir
  51. * fsync foo/some_dir/some_file
  52. *
  53. * The fsync above will unlink the original some_dir without recording
  54. * it in its new location (foo2). After a crash, some_dir will be gone
  55. * unless the fsync of some_file forces a full commit
  56. *
  57. * 2) we must log any new names for any file or dir that is in the fsync
  58. * log. ---> check inode while renaming/linking.
  59. *
  60. * 2a) we must log any new names for any file or dir during rename
  61. * when the directory they are being removed from was logged.
  62. * ---> check inode and old parent dir during rename
  63. *
  64. * 2a is actually the more important variant. With the extra logging
  65. * a crash might unlink the old name without recreating the new one
  66. *
  67. * 3) after a crash, we must go through any directories with a link count
  68. * of zero and redo the rm -rf
  69. *
  70. * mkdir f1/foo
  71. * normal commit
  72. * rm -rf f1/foo
  73. * fsync(f1)
  74. *
  75. * The directory f1 was fully removed from the FS, but fsync was never
  76. * called on f1, only its parent dir. After a crash the rm -rf must
  77. * be replayed. This must be able to recurse down the entire
  78. * directory tree. The inode link count fixup code takes care of the
  79. * ugly details.
  80. */
  81. /*
  82. * stages for the tree walking. The first
  83. * stage (0) is to only pin down the blocks we find
  84. * the second stage (1) is to make sure that all the inodes
  85. * we find in the log are created in the subvolume.
  86. *
  87. * The last stage is to deal with directories and links and extents
  88. * and all the other fun semantics
  89. */
  90. #define LOG_WALK_PIN_ONLY 0
  91. #define LOG_WALK_REPLAY_INODES 1
  92. #define LOG_WALK_REPLAY_DIR_INDEX 2
  93. #define LOG_WALK_REPLAY_ALL 3
  94. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  95. struct btrfs_root *root, struct btrfs_inode *inode,
  96. int inode_only,
  97. const loff_t start,
  98. const loff_t end,
  99. struct btrfs_log_ctx *ctx);
  100. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  101. struct btrfs_root *root,
  102. struct btrfs_path *path, u64 objectid);
  103. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  104. struct btrfs_root *root,
  105. struct btrfs_root *log,
  106. struct btrfs_path *path,
  107. u64 dirid, int del_all);
  108. /*
  109. * tree logging is a special write ahead log used to make sure that
  110. * fsyncs and O_SYNCs can happen without doing full tree commits.
  111. *
  112. * Full tree commits are expensive because they require commonly
  113. * modified blocks to be recowed, creating many dirty pages in the
  114. * extent tree an 4x-6x higher write load than ext3.
  115. *
  116. * Instead of doing a tree commit on every fsync, we use the
  117. * key ranges and transaction ids to find items for a given file or directory
  118. * that have changed in this transaction. Those items are copied into
  119. * a special tree (one per subvolume root), that tree is written to disk
  120. * and then the fsync is considered complete.
  121. *
  122. * After a crash, items are copied out of the log-tree back into the
  123. * subvolume tree. Any file data extents found are recorded in the extent
  124. * allocation tree, and the log-tree freed.
  125. *
  126. * The log tree is read three times, once to pin down all the extents it is
  127. * using in ram and once, once to create all the inodes logged in the tree
  128. * and once to do all the other items.
  129. */
  130. /*
  131. * start a sub transaction and setup the log tree
  132. * this increments the log tree writer count to make the people
  133. * syncing the tree wait for us to finish
  134. */
  135. static int start_log_trans(struct btrfs_trans_handle *trans,
  136. struct btrfs_root *root,
  137. struct btrfs_log_ctx *ctx)
  138. {
  139. struct btrfs_fs_info *fs_info = root->fs_info;
  140. int ret = 0;
  141. mutex_lock(&root->log_mutex);
  142. if (root->log_root) {
  143. if (btrfs_need_log_full_commit(fs_info, trans)) {
  144. ret = -EAGAIN;
  145. goto out;
  146. }
  147. if (!root->log_start_pid) {
  148. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  149. root->log_start_pid = current->pid;
  150. } else if (root->log_start_pid != current->pid) {
  151. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  152. }
  153. } else {
  154. mutex_lock(&fs_info->tree_log_mutex);
  155. if (!fs_info->log_root_tree)
  156. ret = btrfs_init_log_root_tree(trans, fs_info);
  157. mutex_unlock(&fs_info->tree_log_mutex);
  158. if (ret)
  159. goto out;
  160. ret = btrfs_add_log_tree(trans, root);
  161. if (ret)
  162. goto out;
  163. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  164. root->log_start_pid = current->pid;
  165. }
  166. atomic_inc(&root->log_batch);
  167. atomic_inc(&root->log_writers);
  168. if (ctx) {
  169. int index = root->log_transid % 2;
  170. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  171. ctx->log_transid = root->log_transid;
  172. }
  173. out:
  174. mutex_unlock(&root->log_mutex);
  175. return ret;
  176. }
  177. /*
  178. * returns 0 if there was a log transaction running and we were able
  179. * to join, or returns -ENOENT if there were not transactions
  180. * in progress
  181. */
  182. static int join_running_log_trans(struct btrfs_root *root)
  183. {
  184. int ret = -ENOENT;
  185. smp_mb();
  186. if (!root->log_root)
  187. return -ENOENT;
  188. mutex_lock(&root->log_mutex);
  189. if (root->log_root) {
  190. ret = 0;
  191. atomic_inc(&root->log_writers);
  192. }
  193. mutex_unlock(&root->log_mutex);
  194. return ret;
  195. }
  196. /*
  197. * This either makes the current running log transaction wait
  198. * until you call btrfs_end_log_trans() or it makes any future
  199. * log transactions wait until you call btrfs_end_log_trans()
  200. */
  201. int btrfs_pin_log_trans(struct btrfs_root *root)
  202. {
  203. int ret = -ENOENT;
  204. mutex_lock(&root->log_mutex);
  205. atomic_inc(&root->log_writers);
  206. mutex_unlock(&root->log_mutex);
  207. return ret;
  208. }
  209. /*
  210. * indicate we're done making changes to the log tree
  211. * and wake up anyone waiting to do a sync
  212. */
  213. void btrfs_end_log_trans(struct btrfs_root *root)
  214. {
  215. if (atomic_dec_and_test(&root->log_writers)) {
  216. /*
  217. * Implicit memory barrier after atomic_dec_and_test
  218. */
  219. if (waitqueue_active(&root->log_writer_wait))
  220. wake_up(&root->log_writer_wait);
  221. }
  222. }
  223. /*
  224. * the walk control struct is used to pass state down the chain when
  225. * processing the log tree. The stage field tells us which part
  226. * of the log tree processing we are currently doing. The others
  227. * are state fields used for that specific part
  228. */
  229. struct walk_control {
  230. /* should we free the extent on disk when done? This is used
  231. * at transaction commit time while freeing a log tree
  232. */
  233. int free;
  234. /* should we write out the extent buffer? This is used
  235. * while flushing the log tree to disk during a sync
  236. */
  237. int write;
  238. /* should we wait for the extent buffer io to finish? Also used
  239. * while flushing the log tree to disk for a sync
  240. */
  241. int wait;
  242. /* pin only walk, we record which extents on disk belong to the
  243. * log trees
  244. */
  245. int pin;
  246. /* what stage of the replay code we're currently in */
  247. int stage;
  248. /* the root we are currently replaying */
  249. struct btrfs_root *replay_dest;
  250. /* the trans handle for the current replay */
  251. struct btrfs_trans_handle *trans;
  252. /* the function that gets used to process blocks we find in the
  253. * tree. Note the extent_buffer might not be up to date when it is
  254. * passed in, and it must be checked or read if you need the data
  255. * inside it
  256. */
  257. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  258. struct walk_control *wc, u64 gen);
  259. };
  260. /*
  261. * process_func used to pin down extents, write them or wait on them
  262. */
  263. static int process_one_buffer(struct btrfs_root *log,
  264. struct extent_buffer *eb,
  265. struct walk_control *wc, u64 gen)
  266. {
  267. struct btrfs_fs_info *fs_info = log->fs_info;
  268. int ret = 0;
  269. /*
  270. * If this fs is mixed then we need to be able to process the leaves to
  271. * pin down any logged extents, so we have to read the block.
  272. */
  273. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  274. ret = btrfs_read_buffer(eb, gen);
  275. if (ret)
  276. return ret;
  277. }
  278. if (wc->pin)
  279. ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
  280. eb->len);
  281. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  282. if (wc->pin && btrfs_header_level(eb) == 0)
  283. ret = btrfs_exclude_logged_extents(fs_info, eb);
  284. if (wc->write)
  285. btrfs_write_tree_block(eb);
  286. if (wc->wait)
  287. btrfs_wait_tree_block_writeback(eb);
  288. }
  289. return ret;
  290. }
  291. /*
  292. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  293. * to the src data we are copying out.
  294. *
  295. * root is the tree we are copying into, and path is a scratch
  296. * path for use in this function (it should be released on entry and
  297. * will be released on exit).
  298. *
  299. * If the key is already in the destination tree the existing item is
  300. * overwritten. If the existing item isn't big enough, it is extended.
  301. * If it is too large, it is truncated.
  302. *
  303. * If the key isn't in the destination yet, a new item is inserted.
  304. */
  305. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  306. struct btrfs_root *root,
  307. struct btrfs_path *path,
  308. struct extent_buffer *eb, int slot,
  309. struct btrfs_key *key)
  310. {
  311. struct btrfs_fs_info *fs_info = root->fs_info;
  312. int ret;
  313. u32 item_size;
  314. u64 saved_i_size = 0;
  315. int save_old_i_size = 0;
  316. unsigned long src_ptr;
  317. unsigned long dst_ptr;
  318. int overwrite_root = 0;
  319. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  320. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  321. overwrite_root = 1;
  322. item_size = btrfs_item_size_nr(eb, slot);
  323. src_ptr = btrfs_item_ptr_offset(eb, slot);
  324. /* look for the key in the destination tree */
  325. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  326. if (ret < 0)
  327. return ret;
  328. if (ret == 0) {
  329. char *src_copy;
  330. char *dst_copy;
  331. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  332. path->slots[0]);
  333. if (dst_size != item_size)
  334. goto insert;
  335. if (item_size == 0) {
  336. btrfs_release_path(path);
  337. return 0;
  338. }
  339. dst_copy = kmalloc(item_size, GFP_NOFS);
  340. src_copy = kmalloc(item_size, GFP_NOFS);
  341. if (!dst_copy || !src_copy) {
  342. btrfs_release_path(path);
  343. kfree(dst_copy);
  344. kfree(src_copy);
  345. return -ENOMEM;
  346. }
  347. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  348. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  349. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  350. item_size);
  351. ret = memcmp(dst_copy, src_copy, item_size);
  352. kfree(dst_copy);
  353. kfree(src_copy);
  354. /*
  355. * they have the same contents, just return, this saves
  356. * us from cowing blocks in the destination tree and doing
  357. * extra writes that may not have been done by a previous
  358. * sync
  359. */
  360. if (ret == 0) {
  361. btrfs_release_path(path);
  362. return 0;
  363. }
  364. /*
  365. * We need to load the old nbytes into the inode so when we
  366. * replay the extents we've logged we get the right nbytes.
  367. */
  368. if (inode_item) {
  369. struct btrfs_inode_item *item;
  370. u64 nbytes;
  371. u32 mode;
  372. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  373. struct btrfs_inode_item);
  374. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  375. item = btrfs_item_ptr(eb, slot,
  376. struct btrfs_inode_item);
  377. btrfs_set_inode_nbytes(eb, item, nbytes);
  378. /*
  379. * If this is a directory we need to reset the i_size to
  380. * 0 so that we can set it up properly when replaying
  381. * the rest of the items in this log.
  382. */
  383. mode = btrfs_inode_mode(eb, item);
  384. if (S_ISDIR(mode))
  385. btrfs_set_inode_size(eb, item, 0);
  386. }
  387. } else if (inode_item) {
  388. struct btrfs_inode_item *item;
  389. u32 mode;
  390. /*
  391. * New inode, set nbytes to 0 so that the nbytes comes out
  392. * properly when we replay the extents.
  393. */
  394. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  395. btrfs_set_inode_nbytes(eb, item, 0);
  396. /*
  397. * If this is a directory we need to reset the i_size to 0 so
  398. * that we can set it up properly when replaying the rest of
  399. * the items in this log.
  400. */
  401. mode = btrfs_inode_mode(eb, item);
  402. if (S_ISDIR(mode))
  403. btrfs_set_inode_size(eb, item, 0);
  404. }
  405. insert:
  406. btrfs_release_path(path);
  407. /* try to insert the key into the destination tree */
  408. path->skip_release_on_error = 1;
  409. ret = btrfs_insert_empty_item(trans, root, path,
  410. key, item_size);
  411. path->skip_release_on_error = 0;
  412. /* make sure any existing item is the correct size */
  413. if (ret == -EEXIST || ret == -EOVERFLOW) {
  414. u32 found_size;
  415. found_size = btrfs_item_size_nr(path->nodes[0],
  416. path->slots[0]);
  417. if (found_size > item_size)
  418. btrfs_truncate_item(fs_info, path, item_size, 1);
  419. else if (found_size < item_size)
  420. btrfs_extend_item(fs_info, path,
  421. item_size - found_size);
  422. } else if (ret) {
  423. return ret;
  424. }
  425. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  426. path->slots[0]);
  427. /* don't overwrite an existing inode if the generation number
  428. * was logged as zero. This is done when the tree logging code
  429. * is just logging an inode to make sure it exists after recovery.
  430. *
  431. * Also, don't overwrite i_size on directories during replay.
  432. * log replay inserts and removes directory items based on the
  433. * state of the tree found in the subvolume, and i_size is modified
  434. * as it goes
  435. */
  436. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  437. struct btrfs_inode_item *src_item;
  438. struct btrfs_inode_item *dst_item;
  439. src_item = (struct btrfs_inode_item *)src_ptr;
  440. dst_item = (struct btrfs_inode_item *)dst_ptr;
  441. if (btrfs_inode_generation(eb, src_item) == 0) {
  442. struct extent_buffer *dst_eb = path->nodes[0];
  443. const u64 ino_size = btrfs_inode_size(eb, src_item);
  444. /*
  445. * For regular files an ino_size == 0 is used only when
  446. * logging that an inode exists, as part of a directory
  447. * fsync, and the inode wasn't fsynced before. In this
  448. * case don't set the size of the inode in the fs/subvol
  449. * tree, otherwise we would be throwing valid data away.
  450. */
  451. if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
  452. S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
  453. ino_size != 0) {
  454. struct btrfs_map_token token;
  455. btrfs_init_map_token(&token);
  456. btrfs_set_token_inode_size(dst_eb, dst_item,
  457. ino_size, &token);
  458. }
  459. goto no_copy;
  460. }
  461. if (overwrite_root &&
  462. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  463. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  464. save_old_i_size = 1;
  465. saved_i_size = btrfs_inode_size(path->nodes[0],
  466. dst_item);
  467. }
  468. }
  469. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  470. src_ptr, item_size);
  471. if (save_old_i_size) {
  472. struct btrfs_inode_item *dst_item;
  473. dst_item = (struct btrfs_inode_item *)dst_ptr;
  474. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  475. }
  476. /* make sure the generation is filled in */
  477. if (key->type == BTRFS_INODE_ITEM_KEY) {
  478. struct btrfs_inode_item *dst_item;
  479. dst_item = (struct btrfs_inode_item *)dst_ptr;
  480. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  481. btrfs_set_inode_generation(path->nodes[0], dst_item,
  482. trans->transid);
  483. }
  484. }
  485. no_copy:
  486. btrfs_mark_buffer_dirty(path->nodes[0]);
  487. btrfs_release_path(path);
  488. return 0;
  489. }
  490. /*
  491. * simple helper to read an inode off the disk from a given root
  492. * This can only be called for subvolume roots and not for the log
  493. */
  494. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  495. u64 objectid)
  496. {
  497. struct btrfs_key key;
  498. struct inode *inode;
  499. key.objectid = objectid;
  500. key.type = BTRFS_INODE_ITEM_KEY;
  501. key.offset = 0;
  502. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  503. if (IS_ERR(inode)) {
  504. inode = NULL;
  505. } else if (is_bad_inode(inode)) {
  506. iput(inode);
  507. inode = NULL;
  508. }
  509. return inode;
  510. }
  511. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  512. * subvolume 'root'. path is released on entry and should be released
  513. * on exit.
  514. *
  515. * extents in the log tree have not been allocated out of the extent
  516. * tree yet. So, this completes the allocation, taking a reference
  517. * as required if the extent already exists or creating a new extent
  518. * if it isn't in the extent allocation tree yet.
  519. *
  520. * The extent is inserted into the file, dropping any existing extents
  521. * from the file that overlap the new one.
  522. */
  523. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  524. struct btrfs_root *root,
  525. struct btrfs_path *path,
  526. struct extent_buffer *eb, int slot,
  527. struct btrfs_key *key)
  528. {
  529. struct btrfs_fs_info *fs_info = root->fs_info;
  530. int found_type;
  531. u64 extent_end;
  532. u64 start = key->offset;
  533. u64 nbytes = 0;
  534. struct btrfs_file_extent_item *item;
  535. struct inode *inode = NULL;
  536. unsigned long size;
  537. int ret = 0;
  538. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  539. found_type = btrfs_file_extent_type(eb, item);
  540. if (found_type == BTRFS_FILE_EXTENT_REG ||
  541. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  542. nbytes = btrfs_file_extent_num_bytes(eb, item);
  543. extent_end = start + nbytes;
  544. /*
  545. * We don't add to the inodes nbytes if we are prealloc or a
  546. * hole.
  547. */
  548. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  549. nbytes = 0;
  550. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  551. size = btrfs_file_extent_inline_len(eb, slot, item);
  552. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  553. extent_end = ALIGN(start + size,
  554. fs_info->sectorsize);
  555. } else {
  556. ret = 0;
  557. goto out;
  558. }
  559. inode = read_one_inode(root, key->objectid);
  560. if (!inode) {
  561. ret = -EIO;
  562. goto out;
  563. }
  564. /*
  565. * first check to see if we already have this extent in the
  566. * file. This must be done before the btrfs_drop_extents run
  567. * so we don't try to drop this extent.
  568. */
  569. ret = btrfs_lookup_file_extent(trans, root, path,
  570. btrfs_ino(BTRFS_I(inode)), start, 0);
  571. if (ret == 0 &&
  572. (found_type == BTRFS_FILE_EXTENT_REG ||
  573. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  574. struct btrfs_file_extent_item cmp1;
  575. struct btrfs_file_extent_item cmp2;
  576. struct btrfs_file_extent_item *existing;
  577. struct extent_buffer *leaf;
  578. leaf = path->nodes[0];
  579. existing = btrfs_item_ptr(leaf, path->slots[0],
  580. struct btrfs_file_extent_item);
  581. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  582. sizeof(cmp1));
  583. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  584. sizeof(cmp2));
  585. /*
  586. * we already have a pointer to this exact extent,
  587. * we don't have to do anything
  588. */
  589. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  590. btrfs_release_path(path);
  591. goto out;
  592. }
  593. }
  594. btrfs_release_path(path);
  595. /* drop any overlapping extents */
  596. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  597. if (ret)
  598. goto out;
  599. if (found_type == BTRFS_FILE_EXTENT_REG ||
  600. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  601. u64 offset;
  602. unsigned long dest_offset;
  603. struct btrfs_key ins;
  604. ret = btrfs_insert_empty_item(trans, root, path, key,
  605. sizeof(*item));
  606. if (ret)
  607. goto out;
  608. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  609. path->slots[0]);
  610. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  611. (unsigned long)item, sizeof(*item));
  612. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  613. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  614. ins.type = BTRFS_EXTENT_ITEM_KEY;
  615. offset = key->offset - btrfs_file_extent_offset(eb, item);
  616. /*
  617. * Manually record dirty extent, as here we did a shallow
  618. * file extent item copy and skip normal backref update,
  619. * but modifying extent tree all by ourselves.
  620. * So need to manually record dirty extent for qgroup,
  621. * as the owner of the file extent changed from log tree
  622. * (doesn't affect qgroup) to fs/file tree(affects qgroup)
  623. */
  624. ret = btrfs_qgroup_trace_extent(trans, fs_info,
  625. btrfs_file_extent_disk_bytenr(eb, item),
  626. btrfs_file_extent_disk_num_bytes(eb, item),
  627. GFP_NOFS);
  628. if (ret < 0)
  629. goto out;
  630. if (ins.objectid > 0) {
  631. u64 csum_start;
  632. u64 csum_end;
  633. LIST_HEAD(ordered_sums);
  634. /*
  635. * is this extent already allocated in the extent
  636. * allocation tree? If so, just add a reference
  637. */
  638. ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
  639. ins.offset);
  640. if (ret == 0) {
  641. ret = btrfs_inc_extent_ref(trans, fs_info,
  642. ins.objectid, ins.offset,
  643. 0, root->root_key.objectid,
  644. key->objectid, offset);
  645. if (ret)
  646. goto out;
  647. } else {
  648. /*
  649. * insert the extent pointer in the extent
  650. * allocation tree
  651. */
  652. ret = btrfs_alloc_logged_file_extent(trans,
  653. fs_info,
  654. root->root_key.objectid,
  655. key->objectid, offset, &ins);
  656. if (ret)
  657. goto out;
  658. }
  659. btrfs_release_path(path);
  660. if (btrfs_file_extent_compression(eb, item)) {
  661. csum_start = ins.objectid;
  662. csum_end = csum_start + ins.offset;
  663. } else {
  664. csum_start = ins.objectid +
  665. btrfs_file_extent_offset(eb, item);
  666. csum_end = csum_start +
  667. btrfs_file_extent_num_bytes(eb, item);
  668. }
  669. ret = btrfs_lookup_csums_range(root->log_root,
  670. csum_start, csum_end - 1,
  671. &ordered_sums, 0);
  672. if (ret)
  673. goto out;
  674. /*
  675. * Now delete all existing cums in the csum root that
  676. * cover our range. We do this because we can have an
  677. * extent that is completely referenced by one file
  678. * extent item and partially referenced by another
  679. * file extent item (like after using the clone or
  680. * extent_same ioctls). In this case if we end up doing
  681. * the replay of the one that partially references the
  682. * extent first, and we do not do the csum deletion
  683. * below, we can get 2 csum items in the csum tree that
  684. * overlap each other. For example, imagine our log has
  685. * the two following file extent items:
  686. *
  687. * key (257 EXTENT_DATA 409600)
  688. * extent data disk byte 12845056 nr 102400
  689. * extent data offset 20480 nr 20480 ram 102400
  690. *
  691. * key (257 EXTENT_DATA 819200)
  692. * extent data disk byte 12845056 nr 102400
  693. * extent data offset 0 nr 102400 ram 102400
  694. *
  695. * Where the second one fully references the 100K extent
  696. * that starts at disk byte 12845056, and the log tree
  697. * has a single csum item that covers the entire range
  698. * of the extent:
  699. *
  700. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  701. *
  702. * After the first file extent item is replayed, the
  703. * csum tree gets the following csum item:
  704. *
  705. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  706. *
  707. * Which covers the 20K sub-range starting at offset 20K
  708. * of our extent. Now when we replay the second file
  709. * extent item, if we do not delete existing csum items
  710. * that cover any of its blocks, we end up getting two
  711. * csum items in our csum tree that overlap each other:
  712. *
  713. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  714. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  715. *
  716. * Which is a problem, because after this anyone trying
  717. * to lookup up for the checksum of any block of our
  718. * extent starting at an offset of 40K or higher, will
  719. * end up looking at the second csum item only, which
  720. * does not contain the checksum for any block starting
  721. * at offset 40K or higher of our extent.
  722. */
  723. while (!list_empty(&ordered_sums)) {
  724. struct btrfs_ordered_sum *sums;
  725. sums = list_entry(ordered_sums.next,
  726. struct btrfs_ordered_sum,
  727. list);
  728. if (!ret)
  729. ret = btrfs_del_csums(trans, fs_info,
  730. sums->bytenr,
  731. sums->len);
  732. if (!ret)
  733. ret = btrfs_csum_file_blocks(trans,
  734. fs_info->csum_root, sums);
  735. list_del(&sums->list);
  736. kfree(sums);
  737. }
  738. if (ret)
  739. goto out;
  740. } else {
  741. btrfs_release_path(path);
  742. }
  743. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  744. /* inline extents are easy, we just overwrite them */
  745. ret = overwrite_item(trans, root, path, eb, slot, key);
  746. if (ret)
  747. goto out;
  748. }
  749. inode_add_bytes(inode, nbytes);
  750. ret = btrfs_update_inode(trans, root, inode);
  751. out:
  752. if (inode)
  753. iput(inode);
  754. return ret;
  755. }
  756. /*
  757. * when cleaning up conflicts between the directory names in the
  758. * subvolume, directory names in the log and directory names in the
  759. * inode back references, we may have to unlink inodes from directories.
  760. *
  761. * This is a helper function to do the unlink of a specific directory
  762. * item
  763. */
  764. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  765. struct btrfs_root *root,
  766. struct btrfs_path *path,
  767. struct btrfs_inode *dir,
  768. struct btrfs_dir_item *di)
  769. {
  770. struct btrfs_fs_info *fs_info = root->fs_info;
  771. struct inode *inode;
  772. char *name;
  773. int name_len;
  774. struct extent_buffer *leaf;
  775. struct btrfs_key location;
  776. int ret;
  777. leaf = path->nodes[0];
  778. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  779. name_len = btrfs_dir_name_len(leaf, di);
  780. name = kmalloc(name_len, GFP_NOFS);
  781. if (!name)
  782. return -ENOMEM;
  783. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  784. btrfs_release_path(path);
  785. inode = read_one_inode(root, location.objectid);
  786. if (!inode) {
  787. ret = -EIO;
  788. goto out;
  789. }
  790. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  791. if (ret)
  792. goto out;
  793. ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
  794. name_len);
  795. if (ret)
  796. goto out;
  797. else
  798. ret = btrfs_run_delayed_items(trans, fs_info);
  799. out:
  800. kfree(name);
  801. iput(inode);
  802. return ret;
  803. }
  804. /*
  805. * helper function to see if a given name and sequence number found
  806. * in an inode back reference are already in a directory and correctly
  807. * point to this inode
  808. */
  809. static noinline int inode_in_dir(struct btrfs_root *root,
  810. struct btrfs_path *path,
  811. u64 dirid, u64 objectid, u64 index,
  812. const char *name, int name_len)
  813. {
  814. struct btrfs_dir_item *di;
  815. struct btrfs_key location;
  816. int match = 0;
  817. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  818. index, name, name_len, 0);
  819. if (di && !IS_ERR(di)) {
  820. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  821. if (location.objectid != objectid)
  822. goto out;
  823. } else
  824. goto out;
  825. btrfs_release_path(path);
  826. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  827. if (di && !IS_ERR(di)) {
  828. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  829. if (location.objectid != objectid)
  830. goto out;
  831. } else
  832. goto out;
  833. match = 1;
  834. out:
  835. btrfs_release_path(path);
  836. return match;
  837. }
  838. /*
  839. * helper function to check a log tree for a named back reference in
  840. * an inode. This is used to decide if a back reference that is
  841. * found in the subvolume conflicts with what we find in the log.
  842. *
  843. * inode backreferences may have multiple refs in a single item,
  844. * during replay we process one reference at a time, and we don't
  845. * want to delete valid links to a file from the subvolume if that
  846. * link is also in the log.
  847. */
  848. static noinline int backref_in_log(struct btrfs_root *log,
  849. struct btrfs_key *key,
  850. u64 ref_objectid,
  851. const char *name, int namelen)
  852. {
  853. struct btrfs_path *path;
  854. struct btrfs_inode_ref *ref;
  855. unsigned long ptr;
  856. unsigned long ptr_end;
  857. unsigned long name_ptr;
  858. int found_name_len;
  859. int item_size;
  860. int ret;
  861. int match = 0;
  862. path = btrfs_alloc_path();
  863. if (!path)
  864. return -ENOMEM;
  865. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  866. if (ret != 0)
  867. goto out;
  868. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  869. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  870. if (btrfs_find_name_in_ext_backref(path, ref_objectid,
  871. name, namelen, NULL))
  872. match = 1;
  873. goto out;
  874. }
  875. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  876. ptr_end = ptr + item_size;
  877. while (ptr < ptr_end) {
  878. ref = (struct btrfs_inode_ref *)ptr;
  879. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  880. if (found_name_len == namelen) {
  881. name_ptr = (unsigned long)(ref + 1);
  882. ret = memcmp_extent_buffer(path->nodes[0], name,
  883. name_ptr, namelen);
  884. if (ret == 0) {
  885. match = 1;
  886. goto out;
  887. }
  888. }
  889. ptr = (unsigned long)(ref + 1) + found_name_len;
  890. }
  891. out:
  892. btrfs_free_path(path);
  893. return match;
  894. }
  895. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  896. struct btrfs_root *root,
  897. struct btrfs_path *path,
  898. struct btrfs_root *log_root,
  899. struct btrfs_inode *dir,
  900. struct btrfs_inode *inode,
  901. struct extent_buffer *eb,
  902. u64 inode_objectid, u64 parent_objectid,
  903. u64 ref_index, char *name, int namelen,
  904. int *search_done)
  905. {
  906. struct btrfs_fs_info *fs_info = root->fs_info;
  907. int ret;
  908. char *victim_name;
  909. int victim_name_len;
  910. struct extent_buffer *leaf;
  911. struct btrfs_dir_item *di;
  912. struct btrfs_key search_key;
  913. struct btrfs_inode_extref *extref;
  914. again:
  915. /* Search old style refs */
  916. search_key.objectid = inode_objectid;
  917. search_key.type = BTRFS_INODE_REF_KEY;
  918. search_key.offset = parent_objectid;
  919. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  920. if (ret == 0) {
  921. struct btrfs_inode_ref *victim_ref;
  922. unsigned long ptr;
  923. unsigned long ptr_end;
  924. leaf = path->nodes[0];
  925. /* are we trying to overwrite a back ref for the root directory
  926. * if so, just jump out, we're done
  927. */
  928. if (search_key.objectid == search_key.offset)
  929. return 1;
  930. /* check all the names in this back reference to see
  931. * if they are in the log. if so, we allow them to stay
  932. * otherwise they must be unlinked as a conflict
  933. */
  934. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  935. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  936. while (ptr < ptr_end) {
  937. victim_ref = (struct btrfs_inode_ref *)ptr;
  938. victim_name_len = btrfs_inode_ref_name_len(leaf,
  939. victim_ref);
  940. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  941. if (!victim_name)
  942. return -ENOMEM;
  943. read_extent_buffer(leaf, victim_name,
  944. (unsigned long)(victim_ref + 1),
  945. victim_name_len);
  946. if (!backref_in_log(log_root, &search_key,
  947. parent_objectid,
  948. victim_name,
  949. victim_name_len)) {
  950. inc_nlink(&inode->vfs_inode);
  951. btrfs_release_path(path);
  952. ret = btrfs_unlink_inode(trans, root, dir, inode,
  953. victim_name, victim_name_len);
  954. kfree(victim_name);
  955. if (ret)
  956. return ret;
  957. ret = btrfs_run_delayed_items(trans, fs_info);
  958. if (ret)
  959. return ret;
  960. *search_done = 1;
  961. goto again;
  962. }
  963. kfree(victim_name);
  964. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  965. }
  966. /*
  967. * NOTE: we have searched root tree and checked the
  968. * corresponding ref, it does not need to check again.
  969. */
  970. *search_done = 1;
  971. }
  972. btrfs_release_path(path);
  973. /* Same search but for extended refs */
  974. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  975. inode_objectid, parent_objectid, 0,
  976. 0);
  977. if (!IS_ERR_OR_NULL(extref)) {
  978. u32 item_size;
  979. u32 cur_offset = 0;
  980. unsigned long base;
  981. struct inode *victim_parent;
  982. leaf = path->nodes[0];
  983. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  984. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  985. while (cur_offset < item_size) {
  986. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  987. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  988. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  989. goto next;
  990. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  991. if (!victim_name)
  992. return -ENOMEM;
  993. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  994. victim_name_len);
  995. search_key.objectid = inode_objectid;
  996. search_key.type = BTRFS_INODE_EXTREF_KEY;
  997. search_key.offset = btrfs_extref_hash(parent_objectid,
  998. victim_name,
  999. victim_name_len);
  1000. ret = 0;
  1001. if (!backref_in_log(log_root, &search_key,
  1002. parent_objectid, victim_name,
  1003. victim_name_len)) {
  1004. ret = -ENOENT;
  1005. victim_parent = read_one_inode(root,
  1006. parent_objectid);
  1007. if (victim_parent) {
  1008. inc_nlink(&inode->vfs_inode);
  1009. btrfs_release_path(path);
  1010. ret = btrfs_unlink_inode(trans, root,
  1011. BTRFS_I(victim_parent),
  1012. inode,
  1013. victim_name,
  1014. victim_name_len);
  1015. if (!ret)
  1016. ret = btrfs_run_delayed_items(
  1017. trans,
  1018. fs_info);
  1019. }
  1020. iput(victim_parent);
  1021. kfree(victim_name);
  1022. if (ret)
  1023. return ret;
  1024. *search_done = 1;
  1025. goto again;
  1026. }
  1027. kfree(victim_name);
  1028. if (ret)
  1029. return ret;
  1030. next:
  1031. cur_offset += victim_name_len + sizeof(*extref);
  1032. }
  1033. *search_done = 1;
  1034. }
  1035. btrfs_release_path(path);
  1036. /* look for a conflicting sequence number */
  1037. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  1038. ref_index, name, namelen, 0);
  1039. if (di && !IS_ERR(di)) {
  1040. ret = drop_one_dir_item(trans, root, path, dir, di);
  1041. if (ret)
  1042. return ret;
  1043. }
  1044. btrfs_release_path(path);
  1045. /* look for a conflicing name */
  1046. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  1047. name, namelen, 0);
  1048. if (di && !IS_ERR(di)) {
  1049. ret = drop_one_dir_item(trans, root, path, dir, di);
  1050. if (ret)
  1051. return ret;
  1052. }
  1053. btrfs_release_path(path);
  1054. return 0;
  1055. }
  1056. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1057. u32 *namelen, char **name, u64 *index,
  1058. u64 *parent_objectid)
  1059. {
  1060. struct btrfs_inode_extref *extref;
  1061. extref = (struct btrfs_inode_extref *)ref_ptr;
  1062. *namelen = btrfs_inode_extref_name_len(eb, extref);
  1063. *name = kmalloc(*namelen, GFP_NOFS);
  1064. if (*name == NULL)
  1065. return -ENOMEM;
  1066. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  1067. *namelen);
  1068. *index = btrfs_inode_extref_index(eb, extref);
  1069. if (parent_objectid)
  1070. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  1071. return 0;
  1072. }
  1073. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1074. u32 *namelen, char **name, u64 *index)
  1075. {
  1076. struct btrfs_inode_ref *ref;
  1077. ref = (struct btrfs_inode_ref *)ref_ptr;
  1078. *namelen = btrfs_inode_ref_name_len(eb, ref);
  1079. *name = kmalloc(*namelen, GFP_NOFS);
  1080. if (*name == NULL)
  1081. return -ENOMEM;
  1082. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  1083. *index = btrfs_inode_ref_index(eb, ref);
  1084. return 0;
  1085. }
  1086. /*
  1087. * replay one inode back reference item found in the log tree.
  1088. * eb, slot and key refer to the buffer and key found in the log tree.
  1089. * root is the destination we are replaying into, and path is for temp
  1090. * use by this function. (it should be released on return).
  1091. */
  1092. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1093. struct btrfs_root *root,
  1094. struct btrfs_root *log,
  1095. struct btrfs_path *path,
  1096. struct extent_buffer *eb, int slot,
  1097. struct btrfs_key *key)
  1098. {
  1099. struct inode *dir = NULL;
  1100. struct inode *inode = NULL;
  1101. unsigned long ref_ptr;
  1102. unsigned long ref_end;
  1103. char *name = NULL;
  1104. int namelen;
  1105. int ret;
  1106. int search_done = 0;
  1107. int log_ref_ver = 0;
  1108. u64 parent_objectid;
  1109. u64 inode_objectid;
  1110. u64 ref_index = 0;
  1111. int ref_struct_size;
  1112. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1113. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1114. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1115. struct btrfs_inode_extref *r;
  1116. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1117. log_ref_ver = 1;
  1118. r = (struct btrfs_inode_extref *)ref_ptr;
  1119. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1120. } else {
  1121. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1122. parent_objectid = key->offset;
  1123. }
  1124. inode_objectid = key->objectid;
  1125. /*
  1126. * it is possible that we didn't log all the parent directories
  1127. * for a given inode. If we don't find the dir, just don't
  1128. * copy the back ref in. The link count fixup code will take
  1129. * care of the rest
  1130. */
  1131. dir = read_one_inode(root, parent_objectid);
  1132. if (!dir) {
  1133. ret = -ENOENT;
  1134. goto out;
  1135. }
  1136. inode = read_one_inode(root, inode_objectid);
  1137. if (!inode) {
  1138. ret = -EIO;
  1139. goto out;
  1140. }
  1141. while (ref_ptr < ref_end) {
  1142. if (log_ref_ver) {
  1143. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1144. &ref_index, &parent_objectid);
  1145. /*
  1146. * parent object can change from one array
  1147. * item to another.
  1148. */
  1149. if (!dir)
  1150. dir = read_one_inode(root, parent_objectid);
  1151. if (!dir) {
  1152. ret = -ENOENT;
  1153. goto out;
  1154. }
  1155. } else {
  1156. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1157. &ref_index);
  1158. }
  1159. if (ret)
  1160. goto out;
  1161. /* if we already have a perfect match, we're done */
  1162. if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
  1163. btrfs_ino(BTRFS_I(inode)), ref_index,
  1164. name, namelen)) {
  1165. /*
  1166. * look for a conflicting back reference in the
  1167. * metadata. if we find one we have to unlink that name
  1168. * of the file before we add our new link. Later on, we
  1169. * overwrite any existing back reference, and we don't
  1170. * want to create dangling pointers in the directory.
  1171. */
  1172. if (!search_done) {
  1173. ret = __add_inode_ref(trans, root, path, log,
  1174. BTRFS_I(dir),
  1175. BTRFS_I(inode), eb,
  1176. inode_objectid,
  1177. parent_objectid,
  1178. ref_index, name, namelen,
  1179. &search_done);
  1180. if (ret) {
  1181. if (ret == 1)
  1182. ret = 0;
  1183. goto out;
  1184. }
  1185. }
  1186. /* insert our name */
  1187. ret = btrfs_add_link(trans, dir, inode, name, namelen,
  1188. 0, ref_index);
  1189. if (ret)
  1190. goto out;
  1191. btrfs_update_inode(trans, root, inode);
  1192. }
  1193. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1194. kfree(name);
  1195. name = NULL;
  1196. if (log_ref_ver) {
  1197. iput(dir);
  1198. dir = NULL;
  1199. }
  1200. }
  1201. /* finally write the back reference in the inode */
  1202. ret = overwrite_item(trans, root, path, eb, slot, key);
  1203. out:
  1204. btrfs_release_path(path);
  1205. kfree(name);
  1206. iput(dir);
  1207. iput(inode);
  1208. return ret;
  1209. }
  1210. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1211. struct btrfs_root *root, u64 ino)
  1212. {
  1213. int ret;
  1214. ret = btrfs_insert_orphan_item(trans, root, ino);
  1215. if (ret == -EEXIST)
  1216. ret = 0;
  1217. return ret;
  1218. }
  1219. static int count_inode_extrefs(struct btrfs_root *root,
  1220. struct btrfs_inode *inode, struct btrfs_path *path)
  1221. {
  1222. int ret = 0;
  1223. int name_len;
  1224. unsigned int nlink = 0;
  1225. u32 item_size;
  1226. u32 cur_offset = 0;
  1227. u64 inode_objectid = btrfs_ino(inode);
  1228. u64 offset = 0;
  1229. unsigned long ptr;
  1230. struct btrfs_inode_extref *extref;
  1231. struct extent_buffer *leaf;
  1232. while (1) {
  1233. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1234. &extref, &offset);
  1235. if (ret)
  1236. break;
  1237. leaf = path->nodes[0];
  1238. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1239. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1240. cur_offset = 0;
  1241. while (cur_offset < item_size) {
  1242. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1243. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1244. nlink++;
  1245. cur_offset += name_len + sizeof(*extref);
  1246. }
  1247. offset++;
  1248. btrfs_release_path(path);
  1249. }
  1250. btrfs_release_path(path);
  1251. if (ret < 0 && ret != -ENOENT)
  1252. return ret;
  1253. return nlink;
  1254. }
  1255. static int count_inode_refs(struct btrfs_root *root,
  1256. struct btrfs_inode *inode, struct btrfs_path *path)
  1257. {
  1258. int ret;
  1259. struct btrfs_key key;
  1260. unsigned int nlink = 0;
  1261. unsigned long ptr;
  1262. unsigned long ptr_end;
  1263. int name_len;
  1264. u64 ino = btrfs_ino(inode);
  1265. key.objectid = ino;
  1266. key.type = BTRFS_INODE_REF_KEY;
  1267. key.offset = (u64)-1;
  1268. while (1) {
  1269. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1270. if (ret < 0)
  1271. break;
  1272. if (ret > 0) {
  1273. if (path->slots[0] == 0)
  1274. break;
  1275. path->slots[0]--;
  1276. }
  1277. process_slot:
  1278. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1279. path->slots[0]);
  1280. if (key.objectid != ino ||
  1281. key.type != BTRFS_INODE_REF_KEY)
  1282. break;
  1283. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1284. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1285. path->slots[0]);
  1286. while (ptr < ptr_end) {
  1287. struct btrfs_inode_ref *ref;
  1288. ref = (struct btrfs_inode_ref *)ptr;
  1289. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1290. ref);
  1291. ptr = (unsigned long)(ref + 1) + name_len;
  1292. nlink++;
  1293. }
  1294. if (key.offset == 0)
  1295. break;
  1296. if (path->slots[0] > 0) {
  1297. path->slots[0]--;
  1298. goto process_slot;
  1299. }
  1300. key.offset--;
  1301. btrfs_release_path(path);
  1302. }
  1303. btrfs_release_path(path);
  1304. return nlink;
  1305. }
  1306. /*
  1307. * There are a few corners where the link count of the file can't
  1308. * be properly maintained during replay. So, instead of adding
  1309. * lots of complexity to the log code, we just scan the backrefs
  1310. * for any file that has been through replay.
  1311. *
  1312. * The scan will update the link count on the inode to reflect the
  1313. * number of back refs found. If it goes down to zero, the iput
  1314. * will free the inode.
  1315. */
  1316. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1317. struct btrfs_root *root,
  1318. struct inode *inode)
  1319. {
  1320. struct btrfs_path *path;
  1321. int ret;
  1322. u64 nlink = 0;
  1323. u64 ino = btrfs_ino(BTRFS_I(inode));
  1324. path = btrfs_alloc_path();
  1325. if (!path)
  1326. return -ENOMEM;
  1327. ret = count_inode_refs(root, BTRFS_I(inode), path);
  1328. if (ret < 0)
  1329. goto out;
  1330. nlink = ret;
  1331. ret = count_inode_extrefs(root, BTRFS_I(inode), path);
  1332. if (ret < 0)
  1333. goto out;
  1334. nlink += ret;
  1335. ret = 0;
  1336. if (nlink != inode->i_nlink) {
  1337. set_nlink(inode, nlink);
  1338. btrfs_update_inode(trans, root, inode);
  1339. }
  1340. BTRFS_I(inode)->index_cnt = (u64)-1;
  1341. if (inode->i_nlink == 0) {
  1342. if (S_ISDIR(inode->i_mode)) {
  1343. ret = replay_dir_deletes(trans, root, NULL, path,
  1344. ino, 1);
  1345. if (ret)
  1346. goto out;
  1347. }
  1348. ret = insert_orphan_item(trans, root, ino);
  1349. }
  1350. out:
  1351. btrfs_free_path(path);
  1352. return ret;
  1353. }
  1354. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1355. struct btrfs_root *root,
  1356. struct btrfs_path *path)
  1357. {
  1358. int ret;
  1359. struct btrfs_key key;
  1360. struct inode *inode;
  1361. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1362. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1363. key.offset = (u64)-1;
  1364. while (1) {
  1365. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1366. if (ret < 0)
  1367. break;
  1368. if (ret == 1) {
  1369. if (path->slots[0] == 0)
  1370. break;
  1371. path->slots[0]--;
  1372. }
  1373. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1374. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1375. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1376. break;
  1377. ret = btrfs_del_item(trans, root, path);
  1378. if (ret)
  1379. goto out;
  1380. btrfs_release_path(path);
  1381. inode = read_one_inode(root, key.offset);
  1382. if (!inode)
  1383. return -EIO;
  1384. ret = fixup_inode_link_count(trans, root, inode);
  1385. iput(inode);
  1386. if (ret)
  1387. goto out;
  1388. /*
  1389. * fixup on a directory may create new entries,
  1390. * make sure we always look for the highset possible
  1391. * offset
  1392. */
  1393. key.offset = (u64)-1;
  1394. }
  1395. ret = 0;
  1396. out:
  1397. btrfs_release_path(path);
  1398. return ret;
  1399. }
  1400. /*
  1401. * record a given inode in the fixup dir so we can check its link
  1402. * count when replay is done. The link count is incremented here
  1403. * so the inode won't go away until we check it
  1404. */
  1405. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1406. struct btrfs_root *root,
  1407. struct btrfs_path *path,
  1408. u64 objectid)
  1409. {
  1410. struct btrfs_key key;
  1411. int ret = 0;
  1412. struct inode *inode;
  1413. inode = read_one_inode(root, objectid);
  1414. if (!inode)
  1415. return -EIO;
  1416. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1417. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1418. key.offset = objectid;
  1419. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1420. btrfs_release_path(path);
  1421. if (ret == 0) {
  1422. if (!inode->i_nlink)
  1423. set_nlink(inode, 1);
  1424. else
  1425. inc_nlink(inode);
  1426. ret = btrfs_update_inode(trans, root, inode);
  1427. } else if (ret == -EEXIST) {
  1428. ret = 0;
  1429. } else {
  1430. BUG(); /* Logic Error */
  1431. }
  1432. iput(inode);
  1433. return ret;
  1434. }
  1435. /*
  1436. * when replaying the log for a directory, we only insert names
  1437. * for inodes that actually exist. This means an fsync on a directory
  1438. * does not implicitly fsync all the new files in it
  1439. */
  1440. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1441. struct btrfs_root *root,
  1442. u64 dirid, u64 index,
  1443. char *name, int name_len,
  1444. struct btrfs_key *location)
  1445. {
  1446. struct inode *inode;
  1447. struct inode *dir;
  1448. int ret;
  1449. inode = read_one_inode(root, location->objectid);
  1450. if (!inode)
  1451. return -ENOENT;
  1452. dir = read_one_inode(root, dirid);
  1453. if (!dir) {
  1454. iput(inode);
  1455. return -EIO;
  1456. }
  1457. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1458. /* FIXME, put inode into FIXUP list */
  1459. iput(inode);
  1460. iput(dir);
  1461. return ret;
  1462. }
  1463. /*
  1464. * Return true if an inode reference exists in the log for the given name,
  1465. * inode and parent inode.
  1466. */
  1467. static bool name_in_log_ref(struct btrfs_root *log_root,
  1468. const char *name, const int name_len,
  1469. const u64 dirid, const u64 ino)
  1470. {
  1471. struct btrfs_key search_key;
  1472. search_key.objectid = ino;
  1473. search_key.type = BTRFS_INODE_REF_KEY;
  1474. search_key.offset = dirid;
  1475. if (backref_in_log(log_root, &search_key, dirid, name, name_len))
  1476. return true;
  1477. search_key.type = BTRFS_INODE_EXTREF_KEY;
  1478. search_key.offset = btrfs_extref_hash(dirid, name, name_len);
  1479. if (backref_in_log(log_root, &search_key, dirid, name, name_len))
  1480. return true;
  1481. return false;
  1482. }
  1483. /*
  1484. * take a single entry in a log directory item and replay it into
  1485. * the subvolume.
  1486. *
  1487. * if a conflicting item exists in the subdirectory already,
  1488. * the inode it points to is unlinked and put into the link count
  1489. * fix up tree.
  1490. *
  1491. * If a name from the log points to a file or directory that does
  1492. * not exist in the FS, it is skipped. fsyncs on directories
  1493. * do not force down inodes inside that directory, just changes to the
  1494. * names or unlinks in a directory.
  1495. *
  1496. * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
  1497. * non-existing inode) and 1 if the name was replayed.
  1498. */
  1499. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1500. struct btrfs_root *root,
  1501. struct btrfs_path *path,
  1502. struct extent_buffer *eb,
  1503. struct btrfs_dir_item *di,
  1504. struct btrfs_key *key)
  1505. {
  1506. char *name;
  1507. int name_len;
  1508. struct btrfs_dir_item *dst_di;
  1509. struct btrfs_key found_key;
  1510. struct btrfs_key log_key;
  1511. struct inode *dir;
  1512. u8 log_type;
  1513. int exists;
  1514. int ret = 0;
  1515. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1516. bool name_added = false;
  1517. dir = read_one_inode(root, key->objectid);
  1518. if (!dir)
  1519. return -EIO;
  1520. name_len = btrfs_dir_name_len(eb, di);
  1521. name = kmalloc(name_len, GFP_NOFS);
  1522. if (!name) {
  1523. ret = -ENOMEM;
  1524. goto out;
  1525. }
  1526. log_type = btrfs_dir_type(eb, di);
  1527. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1528. name_len);
  1529. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1530. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1531. if (exists == 0)
  1532. exists = 1;
  1533. else
  1534. exists = 0;
  1535. btrfs_release_path(path);
  1536. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1537. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1538. name, name_len, 1);
  1539. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1540. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1541. key->objectid,
  1542. key->offset, name,
  1543. name_len, 1);
  1544. } else {
  1545. /* Corruption */
  1546. ret = -EINVAL;
  1547. goto out;
  1548. }
  1549. if (IS_ERR_OR_NULL(dst_di)) {
  1550. /* we need a sequence number to insert, so we only
  1551. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1552. */
  1553. if (key->type != BTRFS_DIR_INDEX_KEY)
  1554. goto out;
  1555. goto insert;
  1556. }
  1557. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1558. /* the existing item matches the logged item */
  1559. if (found_key.objectid == log_key.objectid &&
  1560. found_key.type == log_key.type &&
  1561. found_key.offset == log_key.offset &&
  1562. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1563. update_size = false;
  1564. goto out;
  1565. }
  1566. /*
  1567. * don't drop the conflicting directory entry if the inode
  1568. * for the new entry doesn't exist
  1569. */
  1570. if (!exists)
  1571. goto out;
  1572. ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
  1573. if (ret)
  1574. goto out;
  1575. if (key->type == BTRFS_DIR_INDEX_KEY)
  1576. goto insert;
  1577. out:
  1578. btrfs_release_path(path);
  1579. if (!ret && update_size) {
  1580. btrfs_i_size_write(dir, dir->i_size + name_len * 2);
  1581. ret = btrfs_update_inode(trans, root, dir);
  1582. }
  1583. kfree(name);
  1584. iput(dir);
  1585. if (!ret && name_added)
  1586. ret = 1;
  1587. return ret;
  1588. insert:
  1589. if (name_in_log_ref(root->log_root, name, name_len,
  1590. key->objectid, log_key.objectid)) {
  1591. /* The dentry will be added later. */
  1592. ret = 0;
  1593. update_size = false;
  1594. goto out;
  1595. }
  1596. btrfs_release_path(path);
  1597. ret = insert_one_name(trans, root, key->objectid, key->offset,
  1598. name, name_len, &log_key);
  1599. if (ret && ret != -ENOENT && ret != -EEXIST)
  1600. goto out;
  1601. if (!ret)
  1602. name_added = true;
  1603. update_size = false;
  1604. ret = 0;
  1605. goto out;
  1606. }
  1607. /*
  1608. * find all the names in a directory item and reconcile them into
  1609. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1610. * one name in a directory item, but the same code gets used for
  1611. * both directory index types
  1612. */
  1613. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1614. struct btrfs_root *root,
  1615. struct btrfs_path *path,
  1616. struct extent_buffer *eb, int slot,
  1617. struct btrfs_key *key)
  1618. {
  1619. struct btrfs_fs_info *fs_info = root->fs_info;
  1620. int ret = 0;
  1621. u32 item_size = btrfs_item_size_nr(eb, slot);
  1622. struct btrfs_dir_item *di;
  1623. int name_len;
  1624. unsigned long ptr;
  1625. unsigned long ptr_end;
  1626. struct btrfs_path *fixup_path = NULL;
  1627. ptr = btrfs_item_ptr_offset(eb, slot);
  1628. ptr_end = ptr + item_size;
  1629. while (ptr < ptr_end) {
  1630. di = (struct btrfs_dir_item *)ptr;
  1631. if (verify_dir_item(fs_info, eb, di))
  1632. return -EIO;
  1633. name_len = btrfs_dir_name_len(eb, di);
  1634. ret = replay_one_name(trans, root, path, eb, di, key);
  1635. if (ret < 0)
  1636. break;
  1637. ptr = (unsigned long)(di + 1);
  1638. ptr += name_len;
  1639. /*
  1640. * If this entry refers to a non-directory (directories can not
  1641. * have a link count > 1) and it was added in the transaction
  1642. * that was not committed, make sure we fixup the link count of
  1643. * the inode it the entry points to. Otherwise something like
  1644. * the following would result in a directory pointing to an
  1645. * inode with a wrong link that does not account for this dir
  1646. * entry:
  1647. *
  1648. * mkdir testdir
  1649. * touch testdir/foo
  1650. * touch testdir/bar
  1651. * sync
  1652. *
  1653. * ln testdir/bar testdir/bar_link
  1654. * ln testdir/foo testdir/foo_link
  1655. * xfs_io -c "fsync" testdir/bar
  1656. *
  1657. * <power failure>
  1658. *
  1659. * mount fs, log replay happens
  1660. *
  1661. * File foo would remain with a link count of 1 when it has two
  1662. * entries pointing to it in the directory testdir. This would
  1663. * make it impossible to ever delete the parent directory has
  1664. * it would result in stale dentries that can never be deleted.
  1665. */
  1666. if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
  1667. struct btrfs_key di_key;
  1668. if (!fixup_path) {
  1669. fixup_path = btrfs_alloc_path();
  1670. if (!fixup_path) {
  1671. ret = -ENOMEM;
  1672. break;
  1673. }
  1674. }
  1675. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  1676. ret = link_to_fixup_dir(trans, root, fixup_path,
  1677. di_key.objectid);
  1678. if (ret)
  1679. break;
  1680. }
  1681. ret = 0;
  1682. }
  1683. btrfs_free_path(fixup_path);
  1684. return ret;
  1685. }
  1686. /*
  1687. * directory replay has two parts. There are the standard directory
  1688. * items in the log copied from the subvolume, and range items
  1689. * created in the log while the subvolume was logged.
  1690. *
  1691. * The range items tell us which parts of the key space the log
  1692. * is authoritative for. During replay, if a key in the subvolume
  1693. * directory is in a logged range item, but not actually in the log
  1694. * that means it was deleted from the directory before the fsync
  1695. * and should be removed.
  1696. */
  1697. static noinline int find_dir_range(struct btrfs_root *root,
  1698. struct btrfs_path *path,
  1699. u64 dirid, int key_type,
  1700. u64 *start_ret, u64 *end_ret)
  1701. {
  1702. struct btrfs_key key;
  1703. u64 found_end;
  1704. struct btrfs_dir_log_item *item;
  1705. int ret;
  1706. int nritems;
  1707. if (*start_ret == (u64)-1)
  1708. return 1;
  1709. key.objectid = dirid;
  1710. key.type = key_type;
  1711. key.offset = *start_ret;
  1712. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1713. if (ret < 0)
  1714. goto out;
  1715. if (ret > 0) {
  1716. if (path->slots[0] == 0)
  1717. goto out;
  1718. path->slots[0]--;
  1719. }
  1720. if (ret != 0)
  1721. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1722. if (key.type != key_type || key.objectid != dirid) {
  1723. ret = 1;
  1724. goto next;
  1725. }
  1726. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1727. struct btrfs_dir_log_item);
  1728. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1729. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1730. ret = 0;
  1731. *start_ret = key.offset;
  1732. *end_ret = found_end;
  1733. goto out;
  1734. }
  1735. ret = 1;
  1736. next:
  1737. /* check the next slot in the tree to see if it is a valid item */
  1738. nritems = btrfs_header_nritems(path->nodes[0]);
  1739. path->slots[0]++;
  1740. if (path->slots[0] >= nritems) {
  1741. ret = btrfs_next_leaf(root, path);
  1742. if (ret)
  1743. goto out;
  1744. }
  1745. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1746. if (key.type != key_type || key.objectid != dirid) {
  1747. ret = 1;
  1748. goto out;
  1749. }
  1750. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1751. struct btrfs_dir_log_item);
  1752. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1753. *start_ret = key.offset;
  1754. *end_ret = found_end;
  1755. ret = 0;
  1756. out:
  1757. btrfs_release_path(path);
  1758. return ret;
  1759. }
  1760. /*
  1761. * this looks for a given directory item in the log. If the directory
  1762. * item is not in the log, the item is removed and the inode it points
  1763. * to is unlinked
  1764. */
  1765. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1766. struct btrfs_root *root,
  1767. struct btrfs_root *log,
  1768. struct btrfs_path *path,
  1769. struct btrfs_path *log_path,
  1770. struct inode *dir,
  1771. struct btrfs_key *dir_key)
  1772. {
  1773. struct btrfs_fs_info *fs_info = root->fs_info;
  1774. int ret;
  1775. struct extent_buffer *eb;
  1776. int slot;
  1777. u32 item_size;
  1778. struct btrfs_dir_item *di;
  1779. struct btrfs_dir_item *log_di;
  1780. int name_len;
  1781. unsigned long ptr;
  1782. unsigned long ptr_end;
  1783. char *name;
  1784. struct inode *inode;
  1785. struct btrfs_key location;
  1786. again:
  1787. eb = path->nodes[0];
  1788. slot = path->slots[0];
  1789. item_size = btrfs_item_size_nr(eb, slot);
  1790. ptr = btrfs_item_ptr_offset(eb, slot);
  1791. ptr_end = ptr + item_size;
  1792. while (ptr < ptr_end) {
  1793. di = (struct btrfs_dir_item *)ptr;
  1794. if (verify_dir_item(fs_info, eb, di)) {
  1795. ret = -EIO;
  1796. goto out;
  1797. }
  1798. name_len = btrfs_dir_name_len(eb, di);
  1799. name = kmalloc(name_len, GFP_NOFS);
  1800. if (!name) {
  1801. ret = -ENOMEM;
  1802. goto out;
  1803. }
  1804. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1805. name_len);
  1806. log_di = NULL;
  1807. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1808. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1809. dir_key->objectid,
  1810. name, name_len, 0);
  1811. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1812. log_di = btrfs_lookup_dir_index_item(trans, log,
  1813. log_path,
  1814. dir_key->objectid,
  1815. dir_key->offset,
  1816. name, name_len, 0);
  1817. }
  1818. if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
  1819. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1820. btrfs_release_path(path);
  1821. btrfs_release_path(log_path);
  1822. inode = read_one_inode(root, location.objectid);
  1823. if (!inode) {
  1824. kfree(name);
  1825. return -EIO;
  1826. }
  1827. ret = link_to_fixup_dir(trans, root,
  1828. path, location.objectid);
  1829. if (ret) {
  1830. kfree(name);
  1831. iput(inode);
  1832. goto out;
  1833. }
  1834. inc_nlink(inode);
  1835. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  1836. BTRFS_I(inode), name, name_len);
  1837. if (!ret)
  1838. ret = btrfs_run_delayed_items(trans, fs_info);
  1839. kfree(name);
  1840. iput(inode);
  1841. if (ret)
  1842. goto out;
  1843. /* there might still be more names under this key
  1844. * check and repeat if required
  1845. */
  1846. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1847. 0, 0);
  1848. if (ret == 0)
  1849. goto again;
  1850. ret = 0;
  1851. goto out;
  1852. } else if (IS_ERR(log_di)) {
  1853. kfree(name);
  1854. return PTR_ERR(log_di);
  1855. }
  1856. btrfs_release_path(log_path);
  1857. kfree(name);
  1858. ptr = (unsigned long)(di + 1);
  1859. ptr += name_len;
  1860. }
  1861. ret = 0;
  1862. out:
  1863. btrfs_release_path(path);
  1864. btrfs_release_path(log_path);
  1865. return ret;
  1866. }
  1867. static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
  1868. struct btrfs_root *root,
  1869. struct btrfs_root *log,
  1870. struct btrfs_path *path,
  1871. const u64 ino)
  1872. {
  1873. struct btrfs_key search_key;
  1874. struct btrfs_path *log_path;
  1875. int i;
  1876. int nritems;
  1877. int ret;
  1878. log_path = btrfs_alloc_path();
  1879. if (!log_path)
  1880. return -ENOMEM;
  1881. search_key.objectid = ino;
  1882. search_key.type = BTRFS_XATTR_ITEM_KEY;
  1883. search_key.offset = 0;
  1884. again:
  1885. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  1886. if (ret < 0)
  1887. goto out;
  1888. process_leaf:
  1889. nritems = btrfs_header_nritems(path->nodes[0]);
  1890. for (i = path->slots[0]; i < nritems; i++) {
  1891. struct btrfs_key key;
  1892. struct btrfs_dir_item *di;
  1893. struct btrfs_dir_item *log_di;
  1894. u32 total_size;
  1895. u32 cur;
  1896. btrfs_item_key_to_cpu(path->nodes[0], &key, i);
  1897. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
  1898. ret = 0;
  1899. goto out;
  1900. }
  1901. di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
  1902. total_size = btrfs_item_size_nr(path->nodes[0], i);
  1903. cur = 0;
  1904. while (cur < total_size) {
  1905. u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
  1906. u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
  1907. u32 this_len = sizeof(*di) + name_len + data_len;
  1908. char *name;
  1909. name = kmalloc(name_len, GFP_NOFS);
  1910. if (!name) {
  1911. ret = -ENOMEM;
  1912. goto out;
  1913. }
  1914. read_extent_buffer(path->nodes[0], name,
  1915. (unsigned long)(di + 1), name_len);
  1916. log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
  1917. name, name_len, 0);
  1918. btrfs_release_path(log_path);
  1919. if (!log_di) {
  1920. /* Doesn't exist in log tree, so delete it. */
  1921. btrfs_release_path(path);
  1922. di = btrfs_lookup_xattr(trans, root, path, ino,
  1923. name, name_len, -1);
  1924. kfree(name);
  1925. if (IS_ERR(di)) {
  1926. ret = PTR_ERR(di);
  1927. goto out;
  1928. }
  1929. ASSERT(di);
  1930. ret = btrfs_delete_one_dir_name(trans, root,
  1931. path, di);
  1932. if (ret)
  1933. goto out;
  1934. btrfs_release_path(path);
  1935. search_key = key;
  1936. goto again;
  1937. }
  1938. kfree(name);
  1939. if (IS_ERR(log_di)) {
  1940. ret = PTR_ERR(log_di);
  1941. goto out;
  1942. }
  1943. cur += this_len;
  1944. di = (struct btrfs_dir_item *)((char *)di + this_len);
  1945. }
  1946. }
  1947. ret = btrfs_next_leaf(root, path);
  1948. if (ret > 0)
  1949. ret = 0;
  1950. else if (ret == 0)
  1951. goto process_leaf;
  1952. out:
  1953. btrfs_free_path(log_path);
  1954. btrfs_release_path(path);
  1955. return ret;
  1956. }
  1957. /*
  1958. * deletion replay happens before we copy any new directory items
  1959. * out of the log or out of backreferences from inodes. It
  1960. * scans the log to find ranges of keys that log is authoritative for,
  1961. * and then scans the directory to find items in those ranges that are
  1962. * not present in the log.
  1963. *
  1964. * Anything we don't find in the log is unlinked and removed from the
  1965. * directory.
  1966. */
  1967. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1968. struct btrfs_root *root,
  1969. struct btrfs_root *log,
  1970. struct btrfs_path *path,
  1971. u64 dirid, int del_all)
  1972. {
  1973. u64 range_start;
  1974. u64 range_end;
  1975. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1976. int ret = 0;
  1977. struct btrfs_key dir_key;
  1978. struct btrfs_key found_key;
  1979. struct btrfs_path *log_path;
  1980. struct inode *dir;
  1981. dir_key.objectid = dirid;
  1982. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1983. log_path = btrfs_alloc_path();
  1984. if (!log_path)
  1985. return -ENOMEM;
  1986. dir = read_one_inode(root, dirid);
  1987. /* it isn't an error if the inode isn't there, that can happen
  1988. * because we replay the deletes before we copy in the inode item
  1989. * from the log
  1990. */
  1991. if (!dir) {
  1992. btrfs_free_path(log_path);
  1993. return 0;
  1994. }
  1995. again:
  1996. range_start = 0;
  1997. range_end = 0;
  1998. while (1) {
  1999. if (del_all)
  2000. range_end = (u64)-1;
  2001. else {
  2002. ret = find_dir_range(log, path, dirid, key_type,
  2003. &range_start, &range_end);
  2004. if (ret != 0)
  2005. break;
  2006. }
  2007. dir_key.offset = range_start;
  2008. while (1) {
  2009. int nritems;
  2010. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  2011. 0, 0);
  2012. if (ret < 0)
  2013. goto out;
  2014. nritems = btrfs_header_nritems(path->nodes[0]);
  2015. if (path->slots[0] >= nritems) {
  2016. ret = btrfs_next_leaf(root, path);
  2017. if (ret)
  2018. break;
  2019. }
  2020. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2021. path->slots[0]);
  2022. if (found_key.objectid != dirid ||
  2023. found_key.type != dir_key.type)
  2024. goto next_type;
  2025. if (found_key.offset > range_end)
  2026. break;
  2027. ret = check_item_in_log(trans, root, log, path,
  2028. log_path, dir,
  2029. &found_key);
  2030. if (ret)
  2031. goto out;
  2032. if (found_key.offset == (u64)-1)
  2033. break;
  2034. dir_key.offset = found_key.offset + 1;
  2035. }
  2036. btrfs_release_path(path);
  2037. if (range_end == (u64)-1)
  2038. break;
  2039. range_start = range_end + 1;
  2040. }
  2041. next_type:
  2042. ret = 0;
  2043. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  2044. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2045. dir_key.type = BTRFS_DIR_INDEX_KEY;
  2046. btrfs_release_path(path);
  2047. goto again;
  2048. }
  2049. out:
  2050. btrfs_release_path(path);
  2051. btrfs_free_path(log_path);
  2052. iput(dir);
  2053. return ret;
  2054. }
  2055. /*
  2056. * the process_func used to replay items from the log tree. This
  2057. * gets called in two different stages. The first stage just looks
  2058. * for inodes and makes sure they are all copied into the subvolume.
  2059. *
  2060. * The second stage copies all the other item types from the log into
  2061. * the subvolume. The two stage approach is slower, but gets rid of
  2062. * lots of complexity around inodes referencing other inodes that exist
  2063. * only in the log (references come from either directory items or inode
  2064. * back refs).
  2065. */
  2066. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  2067. struct walk_control *wc, u64 gen)
  2068. {
  2069. int nritems;
  2070. struct btrfs_path *path;
  2071. struct btrfs_root *root = wc->replay_dest;
  2072. struct btrfs_key key;
  2073. int level;
  2074. int i;
  2075. int ret;
  2076. ret = btrfs_read_buffer(eb, gen);
  2077. if (ret)
  2078. return ret;
  2079. level = btrfs_header_level(eb);
  2080. if (level != 0)
  2081. return 0;
  2082. path = btrfs_alloc_path();
  2083. if (!path)
  2084. return -ENOMEM;
  2085. nritems = btrfs_header_nritems(eb);
  2086. for (i = 0; i < nritems; i++) {
  2087. btrfs_item_key_to_cpu(eb, &key, i);
  2088. /* inode keys are done during the first stage */
  2089. if (key.type == BTRFS_INODE_ITEM_KEY &&
  2090. wc->stage == LOG_WALK_REPLAY_INODES) {
  2091. struct btrfs_inode_item *inode_item;
  2092. u32 mode;
  2093. inode_item = btrfs_item_ptr(eb, i,
  2094. struct btrfs_inode_item);
  2095. ret = replay_xattr_deletes(wc->trans, root, log,
  2096. path, key.objectid);
  2097. if (ret)
  2098. break;
  2099. mode = btrfs_inode_mode(eb, inode_item);
  2100. if (S_ISDIR(mode)) {
  2101. ret = replay_dir_deletes(wc->trans,
  2102. root, log, path, key.objectid, 0);
  2103. if (ret)
  2104. break;
  2105. }
  2106. ret = overwrite_item(wc->trans, root, path,
  2107. eb, i, &key);
  2108. if (ret)
  2109. break;
  2110. /* for regular files, make sure corresponding
  2111. * orphan item exist. extents past the new EOF
  2112. * will be truncated later by orphan cleanup.
  2113. */
  2114. if (S_ISREG(mode)) {
  2115. ret = insert_orphan_item(wc->trans, root,
  2116. key.objectid);
  2117. if (ret)
  2118. break;
  2119. }
  2120. ret = link_to_fixup_dir(wc->trans, root,
  2121. path, key.objectid);
  2122. if (ret)
  2123. break;
  2124. }
  2125. if (key.type == BTRFS_DIR_INDEX_KEY &&
  2126. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2127. ret = replay_one_dir_item(wc->trans, root, path,
  2128. eb, i, &key);
  2129. if (ret)
  2130. break;
  2131. }
  2132. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2133. continue;
  2134. /* these keys are simply copied */
  2135. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  2136. ret = overwrite_item(wc->trans, root, path,
  2137. eb, i, &key);
  2138. if (ret)
  2139. break;
  2140. } else if (key.type == BTRFS_INODE_REF_KEY ||
  2141. key.type == BTRFS_INODE_EXTREF_KEY) {
  2142. ret = add_inode_ref(wc->trans, root, log, path,
  2143. eb, i, &key);
  2144. if (ret && ret != -ENOENT)
  2145. break;
  2146. ret = 0;
  2147. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2148. ret = replay_one_extent(wc->trans, root, path,
  2149. eb, i, &key);
  2150. if (ret)
  2151. break;
  2152. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  2153. ret = replay_one_dir_item(wc->trans, root, path,
  2154. eb, i, &key);
  2155. if (ret)
  2156. break;
  2157. }
  2158. }
  2159. btrfs_free_path(path);
  2160. return ret;
  2161. }
  2162. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  2163. struct btrfs_root *root,
  2164. struct btrfs_path *path, int *level,
  2165. struct walk_control *wc)
  2166. {
  2167. struct btrfs_fs_info *fs_info = root->fs_info;
  2168. u64 root_owner;
  2169. u64 bytenr;
  2170. u64 ptr_gen;
  2171. struct extent_buffer *next;
  2172. struct extent_buffer *cur;
  2173. struct extent_buffer *parent;
  2174. u32 blocksize;
  2175. int ret = 0;
  2176. WARN_ON(*level < 0);
  2177. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2178. while (*level > 0) {
  2179. WARN_ON(*level < 0);
  2180. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2181. cur = path->nodes[*level];
  2182. WARN_ON(btrfs_header_level(cur) != *level);
  2183. if (path->slots[*level] >=
  2184. btrfs_header_nritems(cur))
  2185. break;
  2186. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2187. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2188. blocksize = fs_info->nodesize;
  2189. parent = path->nodes[*level];
  2190. root_owner = btrfs_header_owner(parent);
  2191. next = btrfs_find_create_tree_block(fs_info, bytenr);
  2192. if (IS_ERR(next))
  2193. return PTR_ERR(next);
  2194. if (*level == 1) {
  2195. ret = wc->process_func(root, next, wc, ptr_gen);
  2196. if (ret) {
  2197. free_extent_buffer(next);
  2198. return ret;
  2199. }
  2200. path->slots[*level]++;
  2201. if (wc->free) {
  2202. ret = btrfs_read_buffer(next, ptr_gen);
  2203. if (ret) {
  2204. free_extent_buffer(next);
  2205. return ret;
  2206. }
  2207. if (trans) {
  2208. btrfs_tree_lock(next);
  2209. btrfs_set_lock_blocking(next);
  2210. clean_tree_block(fs_info, next);
  2211. btrfs_wait_tree_block_writeback(next);
  2212. btrfs_tree_unlock(next);
  2213. }
  2214. WARN_ON(root_owner !=
  2215. BTRFS_TREE_LOG_OBJECTID);
  2216. ret = btrfs_free_and_pin_reserved_extent(
  2217. fs_info, bytenr,
  2218. blocksize);
  2219. if (ret) {
  2220. free_extent_buffer(next);
  2221. return ret;
  2222. }
  2223. }
  2224. free_extent_buffer(next);
  2225. continue;
  2226. }
  2227. ret = btrfs_read_buffer(next, ptr_gen);
  2228. if (ret) {
  2229. free_extent_buffer(next);
  2230. return ret;
  2231. }
  2232. WARN_ON(*level <= 0);
  2233. if (path->nodes[*level-1])
  2234. free_extent_buffer(path->nodes[*level-1]);
  2235. path->nodes[*level-1] = next;
  2236. *level = btrfs_header_level(next);
  2237. path->slots[*level] = 0;
  2238. cond_resched();
  2239. }
  2240. WARN_ON(*level < 0);
  2241. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2242. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2243. cond_resched();
  2244. return 0;
  2245. }
  2246. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  2247. struct btrfs_root *root,
  2248. struct btrfs_path *path, int *level,
  2249. struct walk_control *wc)
  2250. {
  2251. struct btrfs_fs_info *fs_info = root->fs_info;
  2252. u64 root_owner;
  2253. int i;
  2254. int slot;
  2255. int ret;
  2256. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2257. slot = path->slots[i];
  2258. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2259. path->slots[i]++;
  2260. *level = i;
  2261. WARN_ON(*level == 0);
  2262. return 0;
  2263. } else {
  2264. struct extent_buffer *parent;
  2265. if (path->nodes[*level] == root->node)
  2266. parent = path->nodes[*level];
  2267. else
  2268. parent = path->nodes[*level + 1];
  2269. root_owner = btrfs_header_owner(parent);
  2270. ret = wc->process_func(root, path->nodes[*level], wc,
  2271. btrfs_header_generation(path->nodes[*level]));
  2272. if (ret)
  2273. return ret;
  2274. if (wc->free) {
  2275. struct extent_buffer *next;
  2276. next = path->nodes[*level];
  2277. if (trans) {
  2278. btrfs_tree_lock(next);
  2279. btrfs_set_lock_blocking(next);
  2280. clean_tree_block(fs_info, next);
  2281. btrfs_wait_tree_block_writeback(next);
  2282. btrfs_tree_unlock(next);
  2283. }
  2284. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2285. ret = btrfs_free_and_pin_reserved_extent(
  2286. fs_info,
  2287. path->nodes[*level]->start,
  2288. path->nodes[*level]->len);
  2289. if (ret)
  2290. return ret;
  2291. }
  2292. free_extent_buffer(path->nodes[*level]);
  2293. path->nodes[*level] = NULL;
  2294. *level = i + 1;
  2295. }
  2296. }
  2297. return 1;
  2298. }
  2299. /*
  2300. * drop the reference count on the tree rooted at 'snap'. This traverses
  2301. * the tree freeing any blocks that have a ref count of zero after being
  2302. * decremented.
  2303. */
  2304. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2305. struct btrfs_root *log, struct walk_control *wc)
  2306. {
  2307. struct btrfs_fs_info *fs_info = log->fs_info;
  2308. int ret = 0;
  2309. int wret;
  2310. int level;
  2311. struct btrfs_path *path;
  2312. int orig_level;
  2313. path = btrfs_alloc_path();
  2314. if (!path)
  2315. return -ENOMEM;
  2316. level = btrfs_header_level(log->node);
  2317. orig_level = level;
  2318. path->nodes[level] = log->node;
  2319. extent_buffer_get(log->node);
  2320. path->slots[level] = 0;
  2321. while (1) {
  2322. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2323. if (wret > 0)
  2324. break;
  2325. if (wret < 0) {
  2326. ret = wret;
  2327. goto out;
  2328. }
  2329. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2330. if (wret > 0)
  2331. break;
  2332. if (wret < 0) {
  2333. ret = wret;
  2334. goto out;
  2335. }
  2336. }
  2337. /* was the root node processed? if not, catch it here */
  2338. if (path->nodes[orig_level]) {
  2339. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2340. btrfs_header_generation(path->nodes[orig_level]));
  2341. if (ret)
  2342. goto out;
  2343. if (wc->free) {
  2344. struct extent_buffer *next;
  2345. next = path->nodes[orig_level];
  2346. if (trans) {
  2347. btrfs_tree_lock(next);
  2348. btrfs_set_lock_blocking(next);
  2349. clean_tree_block(fs_info, next);
  2350. btrfs_wait_tree_block_writeback(next);
  2351. btrfs_tree_unlock(next);
  2352. }
  2353. WARN_ON(log->root_key.objectid !=
  2354. BTRFS_TREE_LOG_OBJECTID);
  2355. ret = btrfs_free_and_pin_reserved_extent(fs_info,
  2356. next->start, next->len);
  2357. if (ret)
  2358. goto out;
  2359. }
  2360. }
  2361. out:
  2362. btrfs_free_path(path);
  2363. return ret;
  2364. }
  2365. /*
  2366. * helper function to update the item for a given subvolumes log root
  2367. * in the tree of log roots
  2368. */
  2369. static int update_log_root(struct btrfs_trans_handle *trans,
  2370. struct btrfs_root *log)
  2371. {
  2372. struct btrfs_fs_info *fs_info = log->fs_info;
  2373. int ret;
  2374. if (log->log_transid == 1) {
  2375. /* insert root item on the first sync */
  2376. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2377. &log->root_key, &log->root_item);
  2378. } else {
  2379. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2380. &log->root_key, &log->root_item);
  2381. }
  2382. return ret;
  2383. }
  2384. static void wait_log_commit(struct btrfs_root *root, int transid)
  2385. {
  2386. DEFINE_WAIT(wait);
  2387. int index = transid % 2;
  2388. /*
  2389. * we only allow two pending log transactions at a time,
  2390. * so we know that if ours is more than 2 older than the
  2391. * current transaction, we're done
  2392. */
  2393. do {
  2394. prepare_to_wait(&root->log_commit_wait[index],
  2395. &wait, TASK_UNINTERRUPTIBLE);
  2396. mutex_unlock(&root->log_mutex);
  2397. if (root->log_transid_committed < transid &&
  2398. atomic_read(&root->log_commit[index]))
  2399. schedule();
  2400. finish_wait(&root->log_commit_wait[index], &wait);
  2401. mutex_lock(&root->log_mutex);
  2402. } while (root->log_transid_committed < transid &&
  2403. atomic_read(&root->log_commit[index]));
  2404. }
  2405. static void wait_for_writer(struct btrfs_root *root)
  2406. {
  2407. DEFINE_WAIT(wait);
  2408. while (atomic_read(&root->log_writers)) {
  2409. prepare_to_wait(&root->log_writer_wait,
  2410. &wait, TASK_UNINTERRUPTIBLE);
  2411. mutex_unlock(&root->log_mutex);
  2412. if (atomic_read(&root->log_writers))
  2413. schedule();
  2414. finish_wait(&root->log_writer_wait, &wait);
  2415. mutex_lock(&root->log_mutex);
  2416. }
  2417. }
  2418. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2419. struct btrfs_log_ctx *ctx)
  2420. {
  2421. if (!ctx)
  2422. return;
  2423. mutex_lock(&root->log_mutex);
  2424. list_del_init(&ctx->list);
  2425. mutex_unlock(&root->log_mutex);
  2426. }
  2427. /*
  2428. * Invoked in log mutex context, or be sure there is no other task which
  2429. * can access the list.
  2430. */
  2431. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2432. int index, int error)
  2433. {
  2434. struct btrfs_log_ctx *ctx;
  2435. struct btrfs_log_ctx *safe;
  2436. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2437. list_del_init(&ctx->list);
  2438. ctx->log_ret = error;
  2439. }
  2440. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2441. }
  2442. /*
  2443. * btrfs_sync_log does sends a given tree log down to the disk and
  2444. * updates the super blocks to record it. When this call is done,
  2445. * you know that any inodes previously logged are safely on disk only
  2446. * if it returns 0.
  2447. *
  2448. * Any other return value means you need to call btrfs_commit_transaction.
  2449. * Some of the edge cases for fsyncing directories that have had unlinks
  2450. * or renames done in the past mean that sometimes the only safe
  2451. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2452. * that has happened.
  2453. */
  2454. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2455. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2456. {
  2457. int index1;
  2458. int index2;
  2459. int mark;
  2460. int ret;
  2461. struct btrfs_fs_info *fs_info = root->fs_info;
  2462. struct btrfs_root *log = root->log_root;
  2463. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2464. int log_transid = 0;
  2465. struct btrfs_log_ctx root_log_ctx;
  2466. struct blk_plug plug;
  2467. mutex_lock(&root->log_mutex);
  2468. log_transid = ctx->log_transid;
  2469. if (root->log_transid_committed >= log_transid) {
  2470. mutex_unlock(&root->log_mutex);
  2471. return ctx->log_ret;
  2472. }
  2473. index1 = log_transid % 2;
  2474. if (atomic_read(&root->log_commit[index1])) {
  2475. wait_log_commit(root, log_transid);
  2476. mutex_unlock(&root->log_mutex);
  2477. return ctx->log_ret;
  2478. }
  2479. ASSERT(log_transid == root->log_transid);
  2480. atomic_set(&root->log_commit[index1], 1);
  2481. /* wait for previous tree log sync to complete */
  2482. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2483. wait_log_commit(root, log_transid - 1);
  2484. while (1) {
  2485. int batch = atomic_read(&root->log_batch);
  2486. /* when we're on an ssd, just kick the log commit out */
  2487. if (!btrfs_test_opt(fs_info, SSD) &&
  2488. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2489. mutex_unlock(&root->log_mutex);
  2490. schedule_timeout_uninterruptible(1);
  2491. mutex_lock(&root->log_mutex);
  2492. }
  2493. wait_for_writer(root);
  2494. if (batch == atomic_read(&root->log_batch))
  2495. break;
  2496. }
  2497. /* bail out if we need to do a full commit */
  2498. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2499. ret = -EAGAIN;
  2500. btrfs_free_logged_extents(log, log_transid);
  2501. mutex_unlock(&root->log_mutex);
  2502. goto out;
  2503. }
  2504. if (log_transid % 2 == 0)
  2505. mark = EXTENT_DIRTY;
  2506. else
  2507. mark = EXTENT_NEW;
  2508. /* we start IO on all the marked extents here, but we don't actually
  2509. * wait for them until later.
  2510. */
  2511. blk_start_plug(&plug);
  2512. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  2513. if (ret) {
  2514. blk_finish_plug(&plug);
  2515. btrfs_abort_transaction(trans, ret);
  2516. btrfs_free_logged_extents(log, log_transid);
  2517. btrfs_set_log_full_commit(fs_info, trans);
  2518. mutex_unlock(&root->log_mutex);
  2519. goto out;
  2520. }
  2521. btrfs_set_root_node(&log->root_item, log->node);
  2522. root->log_transid++;
  2523. log->log_transid = root->log_transid;
  2524. root->log_start_pid = 0;
  2525. /*
  2526. * IO has been started, blocks of the log tree have WRITTEN flag set
  2527. * in their headers. new modifications of the log will be written to
  2528. * new positions. so it's safe to allow log writers to go in.
  2529. */
  2530. mutex_unlock(&root->log_mutex);
  2531. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2532. mutex_lock(&log_root_tree->log_mutex);
  2533. atomic_inc(&log_root_tree->log_batch);
  2534. atomic_inc(&log_root_tree->log_writers);
  2535. index2 = log_root_tree->log_transid % 2;
  2536. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2537. root_log_ctx.log_transid = log_root_tree->log_transid;
  2538. mutex_unlock(&log_root_tree->log_mutex);
  2539. ret = update_log_root(trans, log);
  2540. mutex_lock(&log_root_tree->log_mutex);
  2541. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2542. /*
  2543. * Implicit memory barrier after atomic_dec_and_test
  2544. */
  2545. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2546. wake_up(&log_root_tree->log_writer_wait);
  2547. }
  2548. if (ret) {
  2549. if (!list_empty(&root_log_ctx.list))
  2550. list_del_init(&root_log_ctx.list);
  2551. blk_finish_plug(&plug);
  2552. btrfs_set_log_full_commit(fs_info, trans);
  2553. if (ret != -ENOSPC) {
  2554. btrfs_abort_transaction(trans, ret);
  2555. mutex_unlock(&log_root_tree->log_mutex);
  2556. goto out;
  2557. }
  2558. btrfs_wait_tree_log_extents(log, mark);
  2559. btrfs_free_logged_extents(log, log_transid);
  2560. mutex_unlock(&log_root_tree->log_mutex);
  2561. ret = -EAGAIN;
  2562. goto out;
  2563. }
  2564. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2565. blk_finish_plug(&plug);
  2566. list_del_init(&root_log_ctx.list);
  2567. mutex_unlock(&log_root_tree->log_mutex);
  2568. ret = root_log_ctx.log_ret;
  2569. goto out;
  2570. }
  2571. index2 = root_log_ctx.log_transid % 2;
  2572. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2573. blk_finish_plug(&plug);
  2574. ret = btrfs_wait_tree_log_extents(log, mark);
  2575. btrfs_wait_logged_extents(trans, log, log_transid);
  2576. wait_log_commit(log_root_tree,
  2577. root_log_ctx.log_transid);
  2578. mutex_unlock(&log_root_tree->log_mutex);
  2579. if (!ret)
  2580. ret = root_log_ctx.log_ret;
  2581. goto out;
  2582. }
  2583. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2584. atomic_set(&log_root_tree->log_commit[index2], 1);
  2585. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2586. wait_log_commit(log_root_tree,
  2587. root_log_ctx.log_transid - 1);
  2588. }
  2589. wait_for_writer(log_root_tree);
  2590. /*
  2591. * now that we've moved on to the tree of log tree roots,
  2592. * check the full commit flag again
  2593. */
  2594. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2595. blk_finish_plug(&plug);
  2596. btrfs_wait_tree_log_extents(log, mark);
  2597. btrfs_free_logged_extents(log, log_transid);
  2598. mutex_unlock(&log_root_tree->log_mutex);
  2599. ret = -EAGAIN;
  2600. goto out_wake_log_root;
  2601. }
  2602. ret = btrfs_write_marked_extents(fs_info,
  2603. &log_root_tree->dirty_log_pages,
  2604. EXTENT_DIRTY | EXTENT_NEW);
  2605. blk_finish_plug(&plug);
  2606. if (ret) {
  2607. btrfs_set_log_full_commit(fs_info, trans);
  2608. btrfs_abort_transaction(trans, ret);
  2609. btrfs_free_logged_extents(log, log_transid);
  2610. mutex_unlock(&log_root_tree->log_mutex);
  2611. goto out_wake_log_root;
  2612. }
  2613. ret = btrfs_wait_tree_log_extents(log, mark);
  2614. if (!ret)
  2615. ret = btrfs_wait_tree_log_extents(log_root_tree,
  2616. EXTENT_NEW | EXTENT_DIRTY);
  2617. if (ret) {
  2618. btrfs_set_log_full_commit(fs_info, trans);
  2619. btrfs_free_logged_extents(log, log_transid);
  2620. mutex_unlock(&log_root_tree->log_mutex);
  2621. goto out_wake_log_root;
  2622. }
  2623. btrfs_wait_logged_extents(trans, log, log_transid);
  2624. btrfs_set_super_log_root(fs_info->super_for_commit,
  2625. log_root_tree->node->start);
  2626. btrfs_set_super_log_root_level(fs_info->super_for_commit,
  2627. btrfs_header_level(log_root_tree->node));
  2628. log_root_tree->log_transid++;
  2629. mutex_unlock(&log_root_tree->log_mutex);
  2630. /*
  2631. * nobody else is going to jump in and write the the ctree
  2632. * super here because the log_commit atomic below is protecting
  2633. * us. We must be called with a transaction handle pinning
  2634. * the running transaction open, so a full commit can't hop
  2635. * in and cause problems either.
  2636. */
  2637. ret = write_ctree_super(trans, fs_info, 1);
  2638. if (ret) {
  2639. btrfs_set_log_full_commit(fs_info, trans);
  2640. btrfs_abort_transaction(trans, ret);
  2641. goto out_wake_log_root;
  2642. }
  2643. mutex_lock(&root->log_mutex);
  2644. if (root->last_log_commit < log_transid)
  2645. root->last_log_commit = log_transid;
  2646. mutex_unlock(&root->log_mutex);
  2647. out_wake_log_root:
  2648. mutex_lock(&log_root_tree->log_mutex);
  2649. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2650. log_root_tree->log_transid_committed++;
  2651. atomic_set(&log_root_tree->log_commit[index2], 0);
  2652. mutex_unlock(&log_root_tree->log_mutex);
  2653. /*
  2654. * The barrier before waitqueue_active is implied by mutex_unlock
  2655. */
  2656. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2657. wake_up(&log_root_tree->log_commit_wait[index2]);
  2658. out:
  2659. mutex_lock(&root->log_mutex);
  2660. btrfs_remove_all_log_ctxs(root, index1, ret);
  2661. root->log_transid_committed++;
  2662. atomic_set(&root->log_commit[index1], 0);
  2663. mutex_unlock(&root->log_mutex);
  2664. /*
  2665. * The barrier before waitqueue_active is implied by mutex_unlock
  2666. */
  2667. if (waitqueue_active(&root->log_commit_wait[index1]))
  2668. wake_up(&root->log_commit_wait[index1]);
  2669. return ret;
  2670. }
  2671. static void free_log_tree(struct btrfs_trans_handle *trans,
  2672. struct btrfs_root *log)
  2673. {
  2674. int ret;
  2675. u64 start;
  2676. u64 end;
  2677. struct walk_control wc = {
  2678. .free = 1,
  2679. .process_func = process_one_buffer
  2680. };
  2681. ret = walk_log_tree(trans, log, &wc);
  2682. /* I don't think this can happen but just in case */
  2683. if (ret)
  2684. btrfs_abort_transaction(trans, ret);
  2685. while (1) {
  2686. ret = find_first_extent_bit(&log->dirty_log_pages,
  2687. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2688. NULL);
  2689. if (ret)
  2690. break;
  2691. clear_extent_bits(&log->dirty_log_pages, start, end,
  2692. EXTENT_DIRTY | EXTENT_NEW);
  2693. }
  2694. /*
  2695. * We may have short-circuited the log tree with the full commit logic
  2696. * and left ordered extents on our list, so clear these out to keep us
  2697. * from leaking inodes and memory.
  2698. */
  2699. btrfs_free_logged_extents(log, 0);
  2700. btrfs_free_logged_extents(log, 1);
  2701. free_extent_buffer(log->node);
  2702. kfree(log);
  2703. }
  2704. /*
  2705. * free all the extents used by the tree log. This should be called
  2706. * at commit time of the full transaction
  2707. */
  2708. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2709. {
  2710. if (root->log_root) {
  2711. free_log_tree(trans, root->log_root);
  2712. root->log_root = NULL;
  2713. }
  2714. return 0;
  2715. }
  2716. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2717. struct btrfs_fs_info *fs_info)
  2718. {
  2719. if (fs_info->log_root_tree) {
  2720. free_log_tree(trans, fs_info->log_root_tree);
  2721. fs_info->log_root_tree = NULL;
  2722. }
  2723. return 0;
  2724. }
  2725. /*
  2726. * If both a file and directory are logged, and unlinks or renames are
  2727. * mixed in, we have a few interesting corners:
  2728. *
  2729. * create file X in dir Y
  2730. * link file X to X.link in dir Y
  2731. * fsync file X
  2732. * unlink file X but leave X.link
  2733. * fsync dir Y
  2734. *
  2735. * After a crash we would expect only X.link to exist. But file X
  2736. * didn't get fsync'd again so the log has back refs for X and X.link.
  2737. *
  2738. * We solve this by removing directory entries and inode backrefs from the
  2739. * log when a file that was logged in the current transaction is
  2740. * unlinked. Any later fsync will include the updated log entries, and
  2741. * we'll be able to reconstruct the proper directory items from backrefs.
  2742. *
  2743. * This optimizations allows us to avoid relogging the entire inode
  2744. * or the entire directory.
  2745. */
  2746. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2747. struct btrfs_root *root,
  2748. const char *name, int name_len,
  2749. struct btrfs_inode *dir, u64 index)
  2750. {
  2751. struct btrfs_root *log;
  2752. struct btrfs_dir_item *di;
  2753. struct btrfs_path *path;
  2754. int ret;
  2755. int err = 0;
  2756. int bytes_del = 0;
  2757. u64 dir_ino = btrfs_ino(dir);
  2758. if (dir->logged_trans < trans->transid)
  2759. return 0;
  2760. ret = join_running_log_trans(root);
  2761. if (ret)
  2762. return 0;
  2763. mutex_lock(&dir->log_mutex);
  2764. log = root->log_root;
  2765. path = btrfs_alloc_path();
  2766. if (!path) {
  2767. err = -ENOMEM;
  2768. goto out_unlock;
  2769. }
  2770. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2771. name, name_len, -1);
  2772. if (IS_ERR(di)) {
  2773. err = PTR_ERR(di);
  2774. goto fail;
  2775. }
  2776. if (di) {
  2777. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2778. bytes_del += name_len;
  2779. if (ret) {
  2780. err = ret;
  2781. goto fail;
  2782. }
  2783. }
  2784. btrfs_release_path(path);
  2785. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2786. index, name, name_len, -1);
  2787. if (IS_ERR(di)) {
  2788. err = PTR_ERR(di);
  2789. goto fail;
  2790. }
  2791. if (di) {
  2792. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2793. bytes_del += name_len;
  2794. if (ret) {
  2795. err = ret;
  2796. goto fail;
  2797. }
  2798. }
  2799. /* update the directory size in the log to reflect the names
  2800. * we have removed
  2801. */
  2802. if (bytes_del) {
  2803. struct btrfs_key key;
  2804. key.objectid = dir_ino;
  2805. key.offset = 0;
  2806. key.type = BTRFS_INODE_ITEM_KEY;
  2807. btrfs_release_path(path);
  2808. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2809. if (ret < 0) {
  2810. err = ret;
  2811. goto fail;
  2812. }
  2813. if (ret == 0) {
  2814. struct btrfs_inode_item *item;
  2815. u64 i_size;
  2816. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2817. struct btrfs_inode_item);
  2818. i_size = btrfs_inode_size(path->nodes[0], item);
  2819. if (i_size > bytes_del)
  2820. i_size -= bytes_del;
  2821. else
  2822. i_size = 0;
  2823. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2824. btrfs_mark_buffer_dirty(path->nodes[0]);
  2825. } else
  2826. ret = 0;
  2827. btrfs_release_path(path);
  2828. }
  2829. fail:
  2830. btrfs_free_path(path);
  2831. out_unlock:
  2832. mutex_unlock(&dir->log_mutex);
  2833. if (ret == -ENOSPC) {
  2834. btrfs_set_log_full_commit(root->fs_info, trans);
  2835. ret = 0;
  2836. } else if (ret < 0)
  2837. btrfs_abort_transaction(trans, ret);
  2838. btrfs_end_log_trans(root);
  2839. return err;
  2840. }
  2841. /* see comments for btrfs_del_dir_entries_in_log */
  2842. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2843. struct btrfs_root *root,
  2844. const char *name, int name_len,
  2845. struct btrfs_inode *inode, u64 dirid)
  2846. {
  2847. struct btrfs_fs_info *fs_info = root->fs_info;
  2848. struct btrfs_root *log;
  2849. u64 index;
  2850. int ret;
  2851. if (inode->logged_trans < trans->transid)
  2852. return 0;
  2853. ret = join_running_log_trans(root);
  2854. if (ret)
  2855. return 0;
  2856. log = root->log_root;
  2857. mutex_lock(&inode->log_mutex);
  2858. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2859. dirid, &index);
  2860. mutex_unlock(&inode->log_mutex);
  2861. if (ret == -ENOSPC) {
  2862. btrfs_set_log_full_commit(fs_info, trans);
  2863. ret = 0;
  2864. } else if (ret < 0 && ret != -ENOENT)
  2865. btrfs_abort_transaction(trans, ret);
  2866. btrfs_end_log_trans(root);
  2867. return ret;
  2868. }
  2869. /*
  2870. * creates a range item in the log for 'dirid'. first_offset and
  2871. * last_offset tell us which parts of the key space the log should
  2872. * be considered authoritative for.
  2873. */
  2874. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2875. struct btrfs_root *log,
  2876. struct btrfs_path *path,
  2877. int key_type, u64 dirid,
  2878. u64 first_offset, u64 last_offset)
  2879. {
  2880. int ret;
  2881. struct btrfs_key key;
  2882. struct btrfs_dir_log_item *item;
  2883. key.objectid = dirid;
  2884. key.offset = first_offset;
  2885. if (key_type == BTRFS_DIR_ITEM_KEY)
  2886. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2887. else
  2888. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2889. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2890. if (ret)
  2891. return ret;
  2892. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2893. struct btrfs_dir_log_item);
  2894. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2895. btrfs_mark_buffer_dirty(path->nodes[0]);
  2896. btrfs_release_path(path);
  2897. return 0;
  2898. }
  2899. /*
  2900. * log all the items included in the current transaction for a given
  2901. * directory. This also creates the range items in the log tree required
  2902. * to replay anything deleted before the fsync
  2903. */
  2904. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2905. struct btrfs_root *root, struct btrfs_inode *inode,
  2906. struct btrfs_path *path,
  2907. struct btrfs_path *dst_path, int key_type,
  2908. struct btrfs_log_ctx *ctx,
  2909. u64 min_offset, u64 *last_offset_ret)
  2910. {
  2911. struct btrfs_key min_key;
  2912. struct btrfs_root *log = root->log_root;
  2913. struct extent_buffer *src;
  2914. int err = 0;
  2915. int ret;
  2916. int i;
  2917. int nritems;
  2918. u64 first_offset = min_offset;
  2919. u64 last_offset = (u64)-1;
  2920. u64 ino = btrfs_ino(inode);
  2921. log = root->log_root;
  2922. min_key.objectid = ino;
  2923. min_key.type = key_type;
  2924. min_key.offset = min_offset;
  2925. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  2926. /*
  2927. * we didn't find anything from this transaction, see if there
  2928. * is anything at all
  2929. */
  2930. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2931. min_key.objectid = ino;
  2932. min_key.type = key_type;
  2933. min_key.offset = (u64)-1;
  2934. btrfs_release_path(path);
  2935. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2936. if (ret < 0) {
  2937. btrfs_release_path(path);
  2938. return ret;
  2939. }
  2940. ret = btrfs_previous_item(root, path, ino, key_type);
  2941. /* if ret == 0 there are items for this type,
  2942. * create a range to tell us the last key of this type.
  2943. * otherwise, there are no items in this directory after
  2944. * *min_offset, and we create a range to indicate that.
  2945. */
  2946. if (ret == 0) {
  2947. struct btrfs_key tmp;
  2948. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2949. path->slots[0]);
  2950. if (key_type == tmp.type)
  2951. first_offset = max(min_offset, tmp.offset) + 1;
  2952. }
  2953. goto done;
  2954. }
  2955. /* go backward to find any previous key */
  2956. ret = btrfs_previous_item(root, path, ino, key_type);
  2957. if (ret == 0) {
  2958. struct btrfs_key tmp;
  2959. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2960. if (key_type == tmp.type) {
  2961. first_offset = tmp.offset;
  2962. ret = overwrite_item(trans, log, dst_path,
  2963. path->nodes[0], path->slots[0],
  2964. &tmp);
  2965. if (ret) {
  2966. err = ret;
  2967. goto done;
  2968. }
  2969. }
  2970. }
  2971. btrfs_release_path(path);
  2972. /* find the first key from this transaction again */
  2973. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2974. if (WARN_ON(ret != 0))
  2975. goto done;
  2976. /*
  2977. * we have a block from this transaction, log every item in it
  2978. * from our directory
  2979. */
  2980. while (1) {
  2981. struct btrfs_key tmp;
  2982. src = path->nodes[0];
  2983. nritems = btrfs_header_nritems(src);
  2984. for (i = path->slots[0]; i < nritems; i++) {
  2985. struct btrfs_dir_item *di;
  2986. btrfs_item_key_to_cpu(src, &min_key, i);
  2987. if (min_key.objectid != ino || min_key.type != key_type)
  2988. goto done;
  2989. ret = overwrite_item(trans, log, dst_path, src, i,
  2990. &min_key);
  2991. if (ret) {
  2992. err = ret;
  2993. goto done;
  2994. }
  2995. /*
  2996. * We must make sure that when we log a directory entry,
  2997. * the corresponding inode, after log replay, has a
  2998. * matching link count. For example:
  2999. *
  3000. * touch foo
  3001. * mkdir mydir
  3002. * sync
  3003. * ln foo mydir/bar
  3004. * xfs_io -c "fsync" mydir
  3005. * <crash>
  3006. * <mount fs and log replay>
  3007. *
  3008. * Would result in a fsync log that when replayed, our
  3009. * file inode would have a link count of 1, but we get
  3010. * two directory entries pointing to the same inode.
  3011. * After removing one of the names, it would not be
  3012. * possible to remove the other name, which resulted
  3013. * always in stale file handle errors, and would not
  3014. * be possible to rmdir the parent directory, since
  3015. * its i_size could never decrement to the value
  3016. * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
  3017. */
  3018. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3019. btrfs_dir_item_key_to_cpu(src, di, &tmp);
  3020. if (ctx &&
  3021. (btrfs_dir_transid(src, di) == trans->transid ||
  3022. btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
  3023. tmp.type != BTRFS_ROOT_ITEM_KEY)
  3024. ctx->log_new_dentries = true;
  3025. }
  3026. path->slots[0] = nritems;
  3027. /*
  3028. * look ahead to the next item and see if it is also
  3029. * from this directory and from this transaction
  3030. */
  3031. ret = btrfs_next_leaf(root, path);
  3032. if (ret == 1) {
  3033. last_offset = (u64)-1;
  3034. goto done;
  3035. }
  3036. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3037. if (tmp.objectid != ino || tmp.type != key_type) {
  3038. last_offset = (u64)-1;
  3039. goto done;
  3040. }
  3041. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3042. ret = overwrite_item(trans, log, dst_path,
  3043. path->nodes[0], path->slots[0],
  3044. &tmp);
  3045. if (ret)
  3046. err = ret;
  3047. else
  3048. last_offset = tmp.offset;
  3049. goto done;
  3050. }
  3051. }
  3052. done:
  3053. btrfs_release_path(path);
  3054. btrfs_release_path(dst_path);
  3055. if (err == 0) {
  3056. *last_offset_ret = last_offset;
  3057. /*
  3058. * insert the log range keys to indicate where the log
  3059. * is valid
  3060. */
  3061. ret = insert_dir_log_key(trans, log, path, key_type,
  3062. ino, first_offset, last_offset);
  3063. if (ret)
  3064. err = ret;
  3065. }
  3066. return err;
  3067. }
  3068. /*
  3069. * logging directories is very similar to logging inodes, We find all the items
  3070. * from the current transaction and write them to the log.
  3071. *
  3072. * The recovery code scans the directory in the subvolume, and if it finds a
  3073. * key in the range logged that is not present in the log tree, then it means
  3074. * that dir entry was unlinked during the transaction.
  3075. *
  3076. * In order for that scan to work, we must include one key smaller than
  3077. * the smallest logged by this transaction and one key larger than the largest
  3078. * key logged by this transaction.
  3079. */
  3080. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3081. struct btrfs_root *root, struct btrfs_inode *inode,
  3082. struct btrfs_path *path,
  3083. struct btrfs_path *dst_path,
  3084. struct btrfs_log_ctx *ctx)
  3085. {
  3086. u64 min_key;
  3087. u64 max_key;
  3088. int ret;
  3089. int key_type = BTRFS_DIR_ITEM_KEY;
  3090. again:
  3091. min_key = 0;
  3092. max_key = 0;
  3093. while (1) {
  3094. ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
  3095. ctx, min_key, &max_key);
  3096. if (ret)
  3097. return ret;
  3098. if (max_key == (u64)-1)
  3099. break;
  3100. min_key = max_key + 1;
  3101. }
  3102. if (key_type == BTRFS_DIR_ITEM_KEY) {
  3103. key_type = BTRFS_DIR_INDEX_KEY;
  3104. goto again;
  3105. }
  3106. return 0;
  3107. }
  3108. /*
  3109. * a helper function to drop items from the log before we relog an
  3110. * inode. max_key_type indicates the highest item type to remove.
  3111. * This cannot be run for file data extents because it does not
  3112. * free the extents they point to.
  3113. */
  3114. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  3115. struct btrfs_root *log,
  3116. struct btrfs_path *path,
  3117. u64 objectid, int max_key_type)
  3118. {
  3119. int ret;
  3120. struct btrfs_key key;
  3121. struct btrfs_key found_key;
  3122. int start_slot;
  3123. key.objectid = objectid;
  3124. key.type = max_key_type;
  3125. key.offset = (u64)-1;
  3126. while (1) {
  3127. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3128. BUG_ON(ret == 0); /* Logic error */
  3129. if (ret < 0)
  3130. break;
  3131. if (path->slots[0] == 0)
  3132. break;
  3133. path->slots[0]--;
  3134. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3135. path->slots[0]);
  3136. if (found_key.objectid != objectid)
  3137. break;
  3138. found_key.offset = 0;
  3139. found_key.type = 0;
  3140. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  3141. &start_slot);
  3142. ret = btrfs_del_items(trans, log, path, start_slot,
  3143. path->slots[0] - start_slot + 1);
  3144. /*
  3145. * If start slot isn't 0 then we don't need to re-search, we've
  3146. * found the last guy with the objectid in this tree.
  3147. */
  3148. if (ret || start_slot != 0)
  3149. break;
  3150. btrfs_release_path(path);
  3151. }
  3152. btrfs_release_path(path);
  3153. if (ret > 0)
  3154. ret = 0;
  3155. return ret;
  3156. }
  3157. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3158. struct extent_buffer *leaf,
  3159. struct btrfs_inode_item *item,
  3160. struct inode *inode, int log_inode_only,
  3161. u64 logged_isize)
  3162. {
  3163. struct btrfs_map_token token;
  3164. btrfs_init_map_token(&token);
  3165. if (log_inode_only) {
  3166. /* set the generation to zero so the recover code
  3167. * can tell the difference between an logging
  3168. * just to say 'this inode exists' and a logging
  3169. * to say 'update this inode with these values'
  3170. */
  3171. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  3172. btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
  3173. } else {
  3174. btrfs_set_token_inode_generation(leaf, item,
  3175. BTRFS_I(inode)->generation,
  3176. &token);
  3177. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  3178. }
  3179. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3180. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3181. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3182. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3183. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3184. inode->i_atime.tv_sec, &token);
  3185. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3186. inode->i_atime.tv_nsec, &token);
  3187. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3188. inode->i_mtime.tv_sec, &token);
  3189. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3190. inode->i_mtime.tv_nsec, &token);
  3191. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3192. inode->i_ctime.tv_sec, &token);
  3193. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3194. inode->i_ctime.tv_nsec, &token);
  3195. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3196. &token);
  3197. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3198. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3199. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3200. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3201. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3202. }
  3203. static int log_inode_item(struct btrfs_trans_handle *trans,
  3204. struct btrfs_root *log, struct btrfs_path *path,
  3205. struct btrfs_inode *inode)
  3206. {
  3207. struct btrfs_inode_item *inode_item;
  3208. int ret;
  3209. ret = btrfs_insert_empty_item(trans, log, path,
  3210. &inode->location, sizeof(*inode_item));
  3211. if (ret && ret != -EEXIST)
  3212. return ret;
  3213. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3214. struct btrfs_inode_item);
  3215. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  3216. 0, 0);
  3217. btrfs_release_path(path);
  3218. return 0;
  3219. }
  3220. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3221. struct btrfs_inode *inode,
  3222. struct btrfs_path *dst_path,
  3223. struct btrfs_path *src_path, u64 *last_extent,
  3224. int start_slot, int nr, int inode_only,
  3225. u64 logged_isize)
  3226. {
  3227. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  3228. unsigned long src_offset;
  3229. unsigned long dst_offset;
  3230. struct btrfs_root *log = inode->root->log_root;
  3231. struct btrfs_file_extent_item *extent;
  3232. struct btrfs_inode_item *inode_item;
  3233. struct extent_buffer *src = src_path->nodes[0];
  3234. struct btrfs_key first_key, last_key, key;
  3235. int ret;
  3236. struct btrfs_key *ins_keys;
  3237. u32 *ins_sizes;
  3238. char *ins_data;
  3239. int i;
  3240. struct list_head ordered_sums;
  3241. int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
  3242. bool has_extents = false;
  3243. bool need_find_last_extent = true;
  3244. bool done = false;
  3245. INIT_LIST_HEAD(&ordered_sums);
  3246. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3247. nr * sizeof(u32), GFP_NOFS);
  3248. if (!ins_data)
  3249. return -ENOMEM;
  3250. first_key.objectid = (u64)-1;
  3251. ins_sizes = (u32 *)ins_data;
  3252. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3253. for (i = 0; i < nr; i++) {
  3254. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  3255. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  3256. }
  3257. ret = btrfs_insert_empty_items(trans, log, dst_path,
  3258. ins_keys, ins_sizes, nr);
  3259. if (ret) {
  3260. kfree(ins_data);
  3261. return ret;
  3262. }
  3263. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  3264. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  3265. dst_path->slots[0]);
  3266. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  3267. if ((i == (nr - 1)))
  3268. last_key = ins_keys[i];
  3269. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  3270. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  3271. dst_path->slots[0],
  3272. struct btrfs_inode_item);
  3273. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  3274. &inode->vfs_inode,
  3275. inode_only == LOG_INODE_EXISTS,
  3276. logged_isize);
  3277. } else {
  3278. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  3279. src_offset, ins_sizes[i]);
  3280. }
  3281. /*
  3282. * We set need_find_last_extent here in case we know we were
  3283. * processing other items and then walk into the first extent in
  3284. * the inode. If we don't hit an extent then nothing changes,
  3285. * we'll do the last search the next time around.
  3286. */
  3287. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  3288. has_extents = true;
  3289. if (first_key.objectid == (u64)-1)
  3290. first_key = ins_keys[i];
  3291. } else {
  3292. need_find_last_extent = false;
  3293. }
  3294. /* take a reference on file data extents so that truncates
  3295. * or deletes of this inode don't have to relog the inode
  3296. * again
  3297. */
  3298. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  3299. !skip_csum) {
  3300. int found_type;
  3301. extent = btrfs_item_ptr(src, start_slot + i,
  3302. struct btrfs_file_extent_item);
  3303. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  3304. continue;
  3305. found_type = btrfs_file_extent_type(src, extent);
  3306. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3307. u64 ds, dl, cs, cl;
  3308. ds = btrfs_file_extent_disk_bytenr(src,
  3309. extent);
  3310. /* ds == 0 is a hole */
  3311. if (ds == 0)
  3312. continue;
  3313. dl = btrfs_file_extent_disk_num_bytes(src,
  3314. extent);
  3315. cs = btrfs_file_extent_offset(src, extent);
  3316. cl = btrfs_file_extent_num_bytes(src,
  3317. extent);
  3318. if (btrfs_file_extent_compression(src,
  3319. extent)) {
  3320. cs = 0;
  3321. cl = dl;
  3322. }
  3323. ret = btrfs_lookup_csums_range(
  3324. fs_info->csum_root,
  3325. ds + cs, ds + cs + cl - 1,
  3326. &ordered_sums, 0);
  3327. if (ret) {
  3328. btrfs_release_path(dst_path);
  3329. kfree(ins_data);
  3330. return ret;
  3331. }
  3332. }
  3333. }
  3334. }
  3335. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3336. btrfs_release_path(dst_path);
  3337. kfree(ins_data);
  3338. /*
  3339. * we have to do this after the loop above to avoid changing the
  3340. * log tree while trying to change the log tree.
  3341. */
  3342. ret = 0;
  3343. while (!list_empty(&ordered_sums)) {
  3344. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3345. struct btrfs_ordered_sum,
  3346. list);
  3347. if (!ret)
  3348. ret = btrfs_csum_file_blocks(trans, log, sums);
  3349. list_del(&sums->list);
  3350. kfree(sums);
  3351. }
  3352. if (!has_extents)
  3353. return ret;
  3354. if (need_find_last_extent && *last_extent == first_key.offset) {
  3355. /*
  3356. * We don't have any leafs between our current one and the one
  3357. * we processed before that can have file extent items for our
  3358. * inode (and have a generation number smaller than our current
  3359. * transaction id).
  3360. */
  3361. need_find_last_extent = false;
  3362. }
  3363. /*
  3364. * Because we use btrfs_search_forward we could skip leaves that were
  3365. * not modified and then assume *last_extent is valid when it really
  3366. * isn't. So back up to the previous leaf and read the end of the last
  3367. * extent before we go and fill in holes.
  3368. */
  3369. if (need_find_last_extent) {
  3370. u64 len;
  3371. ret = btrfs_prev_leaf(inode->root, src_path);
  3372. if (ret < 0)
  3373. return ret;
  3374. if (ret)
  3375. goto fill_holes;
  3376. if (src_path->slots[0])
  3377. src_path->slots[0]--;
  3378. src = src_path->nodes[0];
  3379. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3380. if (key.objectid != btrfs_ino(inode) ||
  3381. key.type != BTRFS_EXTENT_DATA_KEY)
  3382. goto fill_holes;
  3383. extent = btrfs_item_ptr(src, src_path->slots[0],
  3384. struct btrfs_file_extent_item);
  3385. if (btrfs_file_extent_type(src, extent) ==
  3386. BTRFS_FILE_EXTENT_INLINE) {
  3387. len = btrfs_file_extent_inline_len(src,
  3388. src_path->slots[0],
  3389. extent);
  3390. *last_extent = ALIGN(key.offset + len,
  3391. fs_info->sectorsize);
  3392. } else {
  3393. len = btrfs_file_extent_num_bytes(src, extent);
  3394. *last_extent = key.offset + len;
  3395. }
  3396. }
  3397. fill_holes:
  3398. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3399. * things could have happened
  3400. *
  3401. * 1) A merge could have happened, so we could currently be on a leaf
  3402. * that holds what we were copying in the first place.
  3403. * 2) A split could have happened, and now not all of the items we want
  3404. * are on the same leaf.
  3405. *
  3406. * So we need to adjust how we search for holes, we need to drop the
  3407. * path and re-search for the first extent key we found, and then walk
  3408. * forward until we hit the last one we copied.
  3409. */
  3410. if (need_find_last_extent) {
  3411. /* btrfs_prev_leaf could return 1 without releasing the path */
  3412. btrfs_release_path(src_path);
  3413. ret = btrfs_search_slot(NULL, inode->root, &first_key,
  3414. src_path, 0, 0);
  3415. if (ret < 0)
  3416. return ret;
  3417. ASSERT(ret == 0);
  3418. src = src_path->nodes[0];
  3419. i = src_path->slots[0];
  3420. } else {
  3421. i = start_slot;
  3422. }
  3423. /*
  3424. * Ok so here we need to go through and fill in any holes we may have
  3425. * to make sure that holes are punched for those areas in case they had
  3426. * extents previously.
  3427. */
  3428. while (!done) {
  3429. u64 offset, len;
  3430. u64 extent_end;
  3431. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3432. ret = btrfs_next_leaf(inode->root, src_path);
  3433. if (ret < 0)
  3434. return ret;
  3435. ASSERT(ret == 0);
  3436. src = src_path->nodes[0];
  3437. i = 0;
  3438. }
  3439. btrfs_item_key_to_cpu(src, &key, i);
  3440. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3441. done = true;
  3442. if (key.objectid != btrfs_ino(inode) ||
  3443. key.type != BTRFS_EXTENT_DATA_KEY) {
  3444. i++;
  3445. continue;
  3446. }
  3447. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3448. if (btrfs_file_extent_type(src, extent) ==
  3449. BTRFS_FILE_EXTENT_INLINE) {
  3450. len = btrfs_file_extent_inline_len(src, i, extent);
  3451. extent_end = ALIGN(key.offset + len,
  3452. fs_info->sectorsize);
  3453. } else {
  3454. len = btrfs_file_extent_num_bytes(src, extent);
  3455. extent_end = key.offset + len;
  3456. }
  3457. i++;
  3458. if (*last_extent == key.offset) {
  3459. *last_extent = extent_end;
  3460. continue;
  3461. }
  3462. offset = *last_extent;
  3463. len = key.offset - *last_extent;
  3464. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3465. offset, 0, 0, len, 0, len, 0, 0, 0);
  3466. if (ret)
  3467. break;
  3468. *last_extent = extent_end;
  3469. }
  3470. /*
  3471. * Need to let the callers know we dropped the path so they should
  3472. * re-search.
  3473. */
  3474. if (!ret && need_find_last_extent)
  3475. ret = 1;
  3476. return ret;
  3477. }
  3478. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3479. {
  3480. struct extent_map *em1, *em2;
  3481. em1 = list_entry(a, struct extent_map, list);
  3482. em2 = list_entry(b, struct extent_map, list);
  3483. if (em1->start < em2->start)
  3484. return -1;
  3485. else if (em1->start > em2->start)
  3486. return 1;
  3487. return 0;
  3488. }
  3489. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3490. struct inode *inode,
  3491. struct btrfs_root *root,
  3492. const struct extent_map *em,
  3493. const struct list_head *logged_list,
  3494. bool *ordered_io_error)
  3495. {
  3496. struct btrfs_fs_info *fs_info = root->fs_info;
  3497. struct btrfs_ordered_extent *ordered;
  3498. struct btrfs_root *log = root->log_root;
  3499. u64 mod_start = em->mod_start;
  3500. u64 mod_len = em->mod_len;
  3501. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3502. u64 csum_offset;
  3503. u64 csum_len;
  3504. LIST_HEAD(ordered_sums);
  3505. int ret = 0;
  3506. *ordered_io_error = false;
  3507. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3508. em->block_start == EXTENT_MAP_HOLE)
  3509. return 0;
  3510. /*
  3511. * Wait far any ordered extent that covers our extent map. If it
  3512. * finishes without an error, first check and see if our csums are on
  3513. * our outstanding ordered extents.
  3514. */
  3515. list_for_each_entry(ordered, logged_list, log_list) {
  3516. struct btrfs_ordered_sum *sum;
  3517. if (!mod_len)
  3518. break;
  3519. if (ordered->file_offset + ordered->len <= mod_start ||
  3520. mod_start + mod_len <= ordered->file_offset)
  3521. continue;
  3522. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3523. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3524. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3525. const u64 start = ordered->file_offset;
  3526. const u64 end = ordered->file_offset + ordered->len - 1;
  3527. WARN_ON(ordered->inode != inode);
  3528. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3529. }
  3530. wait_event(ordered->wait,
  3531. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3532. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3533. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3534. /*
  3535. * Clear the AS_EIO/AS_ENOSPC flags from the inode's
  3536. * i_mapping flags, so that the next fsync won't get
  3537. * an outdated io error too.
  3538. */
  3539. filemap_check_errors(inode->i_mapping);
  3540. *ordered_io_error = true;
  3541. break;
  3542. }
  3543. /*
  3544. * We are going to copy all the csums on this ordered extent, so
  3545. * go ahead and adjust mod_start and mod_len in case this
  3546. * ordered extent has already been logged.
  3547. */
  3548. if (ordered->file_offset > mod_start) {
  3549. if (ordered->file_offset + ordered->len >=
  3550. mod_start + mod_len)
  3551. mod_len = ordered->file_offset - mod_start;
  3552. /*
  3553. * If we have this case
  3554. *
  3555. * |--------- logged extent ---------|
  3556. * |----- ordered extent ----|
  3557. *
  3558. * Just don't mess with mod_start and mod_len, we'll
  3559. * just end up logging more csums than we need and it
  3560. * will be ok.
  3561. */
  3562. } else {
  3563. if (ordered->file_offset + ordered->len <
  3564. mod_start + mod_len) {
  3565. mod_len = (mod_start + mod_len) -
  3566. (ordered->file_offset + ordered->len);
  3567. mod_start = ordered->file_offset +
  3568. ordered->len;
  3569. } else {
  3570. mod_len = 0;
  3571. }
  3572. }
  3573. if (skip_csum)
  3574. continue;
  3575. /*
  3576. * To keep us from looping for the above case of an ordered
  3577. * extent that falls inside of the logged extent.
  3578. */
  3579. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3580. &ordered->flags))
  3581. continue;
  3582. list_for_each_entry(sum, &ordered->list, list) {
  3583. ret = btrfs_csum_file_blocks(trans, log, sum);
  3584. if (ret)
  3585. break;
  3586. }
  3587. }
  3588. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3589. return ret;
  3590. if (em->compress_type) {
  3591. csum_offset = 0;
  3592. csum_len = max(em->block_len, em->orig_block_len);
  3593. } else {
  3594. csum_offset = mod_start - em->start;
  3595. csum_len = mod_len;
  3596. }
  3597. /* block start is already adjusted for the file extent offset. */
  3598. ret = btrfs_lookup_csums_range(fs_info->csum_root,
  3599. em->block_start + csum_offset,
  3600. em->block_start + csum_offset +
  3601. csum_len - 1, &ordered_sums, 0);
  3602. if (ret)
  3603. return ret;
  3604. while (!list_empty(&ordered_sums)) {
  3605. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3606. struct btrfs_ordered_sum,
  3607. list);
  3608. if (!ret)
  3609. ret = btrfs_csum_file_blocks(trans, log, sums);
  3610. list_del(&sums->list);
  3611. kfree(sums);
  3612. }
  3613. return ret;
  3614. }
  3615. static int log_one_extent(struct btrfs_trans_handle *trans,
  3616. struct btrfs_inode *inode, struct btrfs_root *root,
  3617. const struct extent_map *em,
  3618. struct btrfs_path *path,
  3619. const struct list_head *logged_list,
  3620. struct btrfs_log_ctx *ctx)
  3621. {
  3622. struct btrfs_root *log = root->log_root;
  3623. struct btrfs_file_extent_item *fi;
  3624. struct extent_buffer *leaf;
  3625. struct btrfs_map_token token;
  3626. struct btrfs_key key;
  3627. u64 extent_offset = em->start - em->orig_start;
  3628. u64 block_len;
  3629. int ret;
  3630. int extent_inserted = 0;
  3631. bool ordered_io_err = false;
  3632. ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
  3633. logged_list, &ordered_io_err);
  3634. if (ret)
  3635. return ret;
  3636. if (ordered_io_err) {
  3637. ctx->io_err = -EIO;
  3638. return 0;
  3639. }
  3640. btrfs_init_map_token(&token);
  3641. ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
  3642. em->start + em->len, NULL, 0, 1,
  3643. sizeof(*fi), &extent_inserted);
  3644. if (ret)
  3645. return ret;
  3646. if (!extent_inserted) {
  3647. key.objectid = btrfs_ino(inode);
  3648. key.type = BTRFS_EXTENT_DATA_KEY;
  3649. key.offset = em->start;
  3650. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3651. sizeof(*fi));
  3652. if (ret)
  3653. return ret;
  3654. }
  3655. leaf = path->nodes[0];
  3656. fi = btrfs_item_ptr(leaf, path->slots[0],
  3657. struct btrfs_file_extent_item);
  3658. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3659. &token);
  3660. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3661. btrfs_set_token_file_extent_type(leaf, fi,
  3662. BTRFS_FILE_EXTENT_PREALLOC,
  3663. &token);
  3664. else
  3665. btrfs_set_token_file_extent_type(leaf, fi,
  3666. BTRFS_FILE_EXTENT_REG,
  3667. &token);
  3668. block_len = max(em->block_len, em->orig_block_len);
  3669. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3670. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3671. em->block_start,
  3672. &token);
  3673. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3674. &token);
  3675. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3676. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3677. em->block_start -
  3678. extent_offset, &token);
  3679. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3680. &token);
  3681. } else {
  3682. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3683. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3684. &token);
  3685. }
  3686. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3687. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3688. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3689. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3690. &token);
  3691. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3692. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3693. btrfs_mark_buffer_dirty(leaf);
  3694. btrfs_release_path(path);
  3695. return ret;
  3696. }
  3697. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3698. struct btrfs_root *root,
  3699. struct btrfs_inode *inode,
  3700. struct btrfs_path *path,
  3701. struct list_head *logged_list,
  3702. struct btrfs_log_ctx *ctx,
  3703. const u64 start,
  3704. const u64 end)
  3705. {
  3706. struct extent_map *em, *n;
  3707. struct list_head extents;
  3708. struct extent_map_tree *tree = &inode->extent_tree;
  3709. u64 test_gen;
  3710. int ret = 0;
  3711. int num = 0;
  3712. INIT_LIST_HEAD(&extents);
  3713. down_write(&inode->dio_sem);
  3714. write_lock(&tree->lock);
  3715. test_gen = root->fs_info->last_trans_committed;
  3716. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3717. list_del_init(&em->list);
  3718. /*
  3719. * Just an arbitrary number, this can be really CPU intensive
  3720. * once we start getting a lot of extents, and really once we
  3721. * have a bunch of extents we just want to commit since it will
  3722. * be faster.
  3723. */
  3724. if (++num > 32768) {
  3725. list_del_init(&tree->modified_extents);
  3726. ret = -EFBIG;
  3727. goto process;
  3728. }
  3729. if (em->generation <= test_gen)
  3730. continue;
  3731. /* Need a ref to keep it from getting evicted from cache */
  3732. atomic_inc(&em->refs);
  3733. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3734. list_add_tail(&em->list, &extents);
  3735. num++;
  3736. }
  3737. list_sort(NULL, &extents, extent_cmp);
  3738. btrfs_get_logged_extents(inode, logged_list, start, end);
  3739. /*
  3740. * Some ordered extents started by fsync might have completed
  3741. * before we could collect them into the list logged_list, which
  3742. * means they're gone, not in our logged_list nor in the inode's
  3743. * ordered tree. We want the application/user space to know an
  3744. * error happened while attempting to persist file data so that
  3745. * it can take proper action. If such error happened, we leave
  3746. * without writing to the log tree and the fsync must report the
  3747. * file data write error and not commit the current transaction.
  3748. */
  3749. ret = filemap_check_errors(inode->vfs_inode.i_mapping);
  3750. if (ret)
  3751. ctx->io_err = ret;
  3752. process:
  3753. while (!list_empty(&extents)) {
  3754. em = list_entry(extents.next, struct extent_map, list);
  3755. list_del_init(&em->list);
  3756. /*
  3757. * If we had an error we just need to delete everybody from our
  3758. * private list.
  3759. */
  3760. if (ret) {
  3761. clear_em_logging(tree, em);
  3762. free_extent_map(em);
  3763. continue;
  3764. }
  3765. write_unlock(&tree->lock);
  3766. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  3767. ctx);
  3768. write_lock(&tree->lock);
  3769. clear_em_logging(tree, em);
  3770. free_extent_map(em);
  3771. }
  3772. WARN_ON(!list_empty(&extents));
  3773. write_unlock(&tree->lock);
  3774. up_write(&inode->dio_sem);
  3775. btrfs_release_path(path);
  3776. return ret;
  3777. }
  3778. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  3779. struct btrfs_path *path, u64 *size_ret)
  3780. {
  3781. struct btrfs_key key;
  3782. int ret;
  3783. key.objectid = btrfs_ino(inode);
  3784. key.type = BTRFS_INODE_ITEM_KEY;
  3785. key.offset = 0;
  3786. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  3787. if (ret < 0) {
  3788. return ret;
  3789. } else if (ret > 0) {
  3790. *size_ret = 0;
  3791. } else {
  3792. struct btrfs_inode_item *item;
  3793. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3794. struct btrfs_inode_item);
  3795. *size_ret = btrfs_inode_size(path->nodes[0], item);
  3796. }
  3797. btrfs_release_path(path);
  3798. return 0;
  3799. }
  3800. /*
  3801. * At the moment we always log all xattrs. This is to figure out at log replay
  3802. * time which xattrs must have their deletion replayed. If a xattr is missing
  3803. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  3804. * because if a xattr is deleted, the inode is fsynced and a power failure
  3805. * happens, causing the log to be replayed the next time the fs is mounted,
  3806. * we want the xattr to not exist anymore (same behaviour as other filesystems
  3807. * with a journal, ext3/4, xfs, f2fs, etc).
  3808. */
  3809. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  3810. struct btrfs_root *root,
  3811. struct btrfs_inode *inode,
  3812. struct btrfs_path *path,
  3813. struct btrfs_path *dst_path)
  3814. {
  3815. int ret;
  3816. struct btrfs_key key;
  3817. const u64 ino = btrfs_ino(inode);
  3818. int ins_nr = 0;
  3819. int start_slot = 0;
  3820. key.objectid = ino;
  3821. key.type = BTRFS_XATTR_ITEM_KEY;
  3822. key.offset = 0;
  3823. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3824. if (ret < 0)
  3825. return ret;
  3826. while (true) {
  3827. int slot = path->slots[0];
  3828. struct extent_buffer *leaf = path->nodes[0];
  3829. int nritems = btrfs_header_nritems(leaf);
  3830. if (slot >= nritems) {
  3831. if (ins_nr > 0) {
  3832. u64 last_extent = 0;
  3833. ret = copy_items(trans, inode, dst_path, path,
  3834. &last_extent, start_slot,
  3835. ins_nr, 1, 0);
  3836. /* can't be 1, extent items aren't processed */
  3837. ASSERT(ret <= 0);
  3838. if (ret < 0)
  3839. return ret;
  3840. ins_nr = 0;
  3841. }
  3842. ret = btrfs_next_leaf(root, path);
  3843. if (ret < 0)
  3844. return ret;
  3845. else if (ret > 0)
  3846. break;
  3847. continue;
  3848. }
  3849. btrfs_item_key_to_cpu(leaf, &key, slot);
  3850. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  3851. break;
  3852. if (ins_nr == 0)
  3853. start_slot = slot;
  3854. ins_nr++;
  3855. path->slots[0]++;
  3856. cond_resched();
  3857. }
  3858. if (ins_nr > 0) {
  3859. u64 last_extent = 0;
  3860. ret = copy_items(trans, inode, dst_path, path,
  3861. &last_extent, start_slot,
  3862. ins_nr, 1, 0);
  3863. /* can't be 1, extent items aren't processed */
  3864. ASSERT(ret <= 0);
  3865. if (ret < 0)
  3866. return ret;
  3867. }
  3868. return 0;
  3869. }
  3870. /*
  3871. * If the no holes feature is enabled we need to make sure any hole between the
  3872. * last extent and the i_size of our inode is explicitly marked in the log. This
  3873. * is to make sure that doing something like:
  3874. *
  3875. * 1) create file with 128Kb of data
  3876. * 2) truncate file to 64Kb
  3877. * 3) truncate file to 256Kb
  3878. * 4) fsync file
  3879. * 5) <crash/power failure>
  3880. * 6) mount fs and trigger log replay
  3881. *
  3882. * Will give us a file with a size of 256Kb, the first 64Kb of data match what
  3883. * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
  3884. * file correspond to a hole. The presence of explicit holes in a log tree is
  3885. * what guarantees that log replay will remove/adjust file extent items in the
  3886. * fs/subvol tree.
  3887. *
  3888. * Here we do not need to care about holes between extents, that is already done
  3889. * by copy_items(). We also only need to do this in the full sync path, where we
  3890. * lookup for extents from the fs/subvol tree only. In the fast path case, we
  3891. * lookup the list of modified extent maps and if any represents a hole, we
  3892. * insert a corresponding extent representing a hole in the log tree.
  3893. */
  3894. static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
  3895. struct btrfs_root *root,
  3896. struct btrfs_inode *inode,
  3897. struct btrfs_path *path)
  3898. {
  3899. struct btrfs_fs_info *fs_info = root->fs_info;
  3900. int ret;
  3901. struct btrfs_key key;
  3902. u64 hole_start;
  3903. u64 hole_size;
  3904. struct extent_buffer *leaf;
  3905. struct btrfs_root *log = root->log_root;
  3906. const u64 ino = btrfs_ino(inode);
  3907. const u64 i_size = i_size_read(&inode->vfs_inode);
  3908. if (!btrfs_fs_incompat(fs_info, NO_HOLES))
  3909. return 0;
  3910. key.objectid = ino;
  3911. key.type = BTRFS_EXTENT_DATA_KEY;
  3912. key.offset = (u64)-1;
  3913. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3914. ASSERT(ret != 0);
  3915. if (ret < 0)
  3916. return ret;
  3917. ASSERT(path->slots[0] > 0);
  3918. path->slots[0]--;
  3919. leaf = path->nodes[0];
  3920. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3921. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
  3922. /* inode does not have any extents */
  3923. hole_start = 0;
  3924. hole_size = i_size;
  3925. } else {
  3926. struct btrfs_file_extent_item *extent;
  3927. u64 len;
  3928. /*
  3929. * If there's an extent beyond i_size, an explicit hole was
  3930. * already inserted by copy_items().
  3931. */
  3932. if (key.offset >= i_size)
  3933. return 0;
  3934. extent = btrfs_item_ptr(leaf, path->slots[0],
  3935. struct btrfs_file_extent_item);
  3936. if (btrfs_file_extent_type(leaf, extent) ==
  3937. BTRFS_FILE_EXTENT_INLINE) {
  3938. len = btrfs_file_extent_inline_len(leaf,
  3939. path->slots[0],
  3940. extent);
  3941. ASSERT(len == i_size);
  3942. return 0;
  3943. }
  3944. len = btrfs_file_extent_num_bytes(leaf, extent);
  3945. /* Last extent goes beyond i_size, no need to log a hole. */
  3946. if (key.offset + len > i_size)
  3947. return 0;
  3948. hole_start = key.offset + len;
  3949. hole_size = i_size - hole_start;
  3950. }
  3951. btrfs_release_path(path);
  3952. /* Last extent ends at i_size. */
  3953. if (hole_size == 0)
  3954. return 0;
  3955. hole_size = ALIGN(hole_size, fs_info->sectorsize);
  3956. ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
  3957. hole_size, 0, hole_size, 0, 0, 0);
  3958. return ret;
  3959. }
  3960. /*
  3961. * When we are logging a new inode X, check if it doesn't have a reference that
  3962. * matches the reference from some other inode Y created in a past transaction
  3963. * and that was renamed in the current transaction. If we don't do this, then at
  3964. * log replay time we can lose inode Y (and all its files if it's a directory):
  3965. *
  3966. * mkdir /mnt/x
  3967. * echo "hello world" > /mnt/x/foobar
  3968. * sync
  3969. * mv /mnt/x /mnt/y
  3970. * mkdir /mnt/x # or touch /mnt/x
  3971. * xfs_io -c fsync /mnt/x
  3972. * <power fail>
  3973. * mount fs, trigger log replay
  3974. *
  3975. * After the log replay procedure, we would lose the first directory and all its
  3976. * files (file foobar).
  3977. * For the case where inode Y is not a directory we simply end up losing it:
  3978. *
  3979. * echo "123" > /mnt/foo
  3980. * sync
  3981. * mv /mnt/foo /mnt/bar
  3982. * echo "abc" > /mnt/foo
  3983. * xfs_io -c fsync /mnt/foo
  3984. * <power fail>
  3985. *
  3986. * We also need this for cases where a snapshot entry is replaced by some other
  3987. * entry (file or directory) otherwise we end up with an unreplayable log due to
  3988. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  3989. * if it were a regular entry:
  3990. *
  3991. * mkdir /mnt/x
  3992. * btrfs subvolume snapshot /mnt /mnt/x/snap
  3993. * btrfs subvolume delete /mnt/x/snap
  3994. * rmdir /mnt/x
  3995. * mkdir /mnt/x
  3996. * fsync /mnt/x or fsync some new file inside it
  3997. * <power fail>
  3998. *
  3999. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4000. * the same transaction.
  4001. */
  4002. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4003. const int slot,
  4004. const struct btrfs_key *key,
  4005. struct btrfs_inode *inode,
  4006. u64 *other_ino)
  4007. {
  4008. int ret;
  4009. struct btrfs_path *search_path;
  4010. char *name = NULL;
  4011. u32 name_len = 0;
  4012. u32 item_size = btrfs_item_size_nr(eb, slot);
  4013. u32 cur_offset = 0;
  4014. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4015. search_path = btrfs_alloc_path();
  4016. if (!search_path)
  4017. return -ENOMEM;
  4018. search_path->search_commit_root = 1;
  4019. search_path->skip_locking = 1;
  4020. while (cur_offset < item_size) {
  4021. u64 parent;
  4022. u32 this_name_len;
  4023. u32 this_len;
  4024. unsigned long name_ptr;
  4025. struct btrfs_dir_item *di;
  4026. if (key->type == BTRFS_INODE_REF_KEY) {
  4027. struct btrfs_inode_ref *iref;
  4028. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4029. parent = key->offset;
  4030. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4031. name_ptr = (unsigned long)(iref + 1);
  4032. this_len = sizeof(*iref) + this_name_len;
  4033. } else {
  4034. struct btrfs_inode_extref *extref;
  4035. extref = (struct btrfs_inode_extref *)(ptr +
  4036. cur_offset);
  4037. parent = btrfs_inode_extref_parent(eb, extref);
  4038. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4039. name_ptr = (unsigned long)&extref->name;
  4040. this_len = sizeof(*extref) + this_name_len;
  4041. }
  4042. if (this_name_len > name_len) {
  4043. char *new_name;
  4044. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4045. if (!new_name) {
  4046. ret = -ENOMEM;
  4047. goto out;
  4048. }
  4049. name_len = this_name_len;
  4050. name = new_name;
  4051. }
  4052. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4053. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4054. parent, name, this_name_len, 0);
  4055. if (di && !IS_ERR(di)) {
  4056. struct btrfs_key di_key;
  4057. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4058. di, &di_key);
  4059. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4060. ret = 1;
  4061. *other_ino = di_key.objectid;
  4062. } else {
  4063. ret = -EAGAIN;
  4064. }
  4065. goto out;
  4066. } else if (IS_ERR(di)) {
  4067. ret = PTR_ERR(di);
  4068. goto out;
  4069. }
  4070. btrfs_release_path(search_path);
  4071. cur_offset += this_len;
  4072. }
  4073. ret = 0;
  4074. out:
  4075. btrfs_free_path(search_path);
  4076. kfree(name);
  4077. return ret;
  4078. }
  4079. /* log a single inode in the tree log.
  4080. * At least one parent directory for this inode must exist in the tree
  4081. * or be logged already.
  4082. *
  4083. * Any items from this inode changed by the current transaction are copied
  4084. * to the log tree. An extra reference is taken on any extents in this
  4085. * file, allowing us to avoid a whole pile of corner cases around logging
  4086. * blocks that have been removed from the tree.
  4087. *
  4088. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4089. * does.
  4090. *
  4091. * This handles both files and directories.
  4092. */
  4093. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4094. struct btrfs_root *root, struct btrfs_inode *inode,
  4095. int inode_only,
  4096. const loff_t start,
  4097. const loff_t end,
  4098. struct btrfs_log_ctx *ctx)
  4099. {
  4100. struct btrfs_fs_info *fs_info = root->fs_info;
  4101. struct btrfs_path *path;
  4102. struct btrfs_path *dst_path;
  4103. struct btrfs_key min_key;
  4104. struct btrfs_key max_key;
  4105. struct btrfs_root *log = root->log_root;
  4106. struct extent_buffer *src = NULL;
  4107. LIST_HEAD(logged_list);
  4108. u64 last_extent = 0;
  4109. int err = 0;
  4110. int ret;
  4111. int nritems;
  4112. int ins_start_slot = 0;
  4113. int ins_nr;
  4114. bool fast_search = false;
  4115. u64 ino = btrfs_ino(inode);
  4116. struct extent_map_tree *em_tree = &inode->extent_tree;
  4117. u64 logged_isize = 0;
  4118. bool need_log_inode_item = true;
  4119. path = btrfs_alloc_path();
  4120. if (!path)
  4121. return -ENOMEM;
  4122. dst_path = btrfs_alloc_path();
  4123. if (!dst_path) {
  4124. btrfs_free_path(path);
  4125. return -ENOMEM;
  4126. }
  4127. min_key.objectid = ino;
  4128. min_key.type = BTRFS_INODE_ITEM_KEY;
  4129. min_key.offset = 0;
  4130. max_key.objectid = ino;
  4131. /* today the code can only do partial logging of directories */
  4132. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4133. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4134. &inode->runtime_flags) &&
  4135. inode_only >= LOG_INODE_EXISTS))
  4136. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4137. else
  4138. max_key.type = (u8)-1;
  4139. max_key.offset = (u64)-1;
  4140. /*
  4141. * Only run delayed items if we are a dir or a new file.
  4142. * Otherwise commit the delayed inode only, which is needed in
  4143. * order for the log replay code to mark inodes for link count
  4144. * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
  4145. */
  4146. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4147. inode->generation > fs_info->last_trans_committed)
  4148. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4149. else
  4150. ret = btrfs_commit_inode_delayed_inode(inode);
  4151. if (ret) {
  4152. btrfs_free_path(path);
  4153. btrfs_free_path(dst_path);
  4154. return ret;
  4155. }
  4156. if (inode_only == LOG_OTHER_INODE) {
  4157. inode_only = LOG_INODE_EXISTS;
  4158. mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
  4159. } else {
  4160. mutex_lock(&inode->log_mutex);
  4161. }
  4162. /*
  4163. * a brute force approach to making sure we get the most uptodate
  4164. * copies of everything.
  4165. */
  4166. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  4167. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4168. if (inode_only == LOG_INODE_EXISTS)
  4169. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4170. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4171. } else {
  4172. if (inode_only == LOG_INODE_EXISTS) {
  4173. /*
  4174. * Make sure the new inode item we write to the log has
  4175. * the same isize as the current one (if it exists).
  4176. * This is necessary to prevent data loss after log
  4177. * replay, and also to prevent doing a wrong expanding
  4178. * truncate - for e.g. create file, write 4K into offset
  4179. * 0, fsync, write 4K into offset 4096, add hard link,
  4180. * fsync some other file (to sync log), power fail - if
  4181. * we use the inode's current i_size, after log replay
  4182. * we get a 8Kb file, with the last 4Kb extent as a hole
  4183. * (zeroes), as if an expanding truncate happened,
  4184. * instead of getting a file of 4Kb only.
  4185. */
  4186. err = logged_inode_size(log, inode, path, &logged_isize);
  4187. if (err)
  4188. goto out_unlock;
  4189. }
  4190. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4191. &inode->runtime_flags)) {
  4192. if (inode_only == LOG_INODE_EXISTS) {
  4193. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4194. ret = drop_objectid_items(trans, log, path, ino,
  4195. max_key.type);
  4196. } else {
  4197. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4198. &inode->runtime_flags);
  4199. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4200. &inode->runtime_flags);
  4201. while(1) {
  4202. ret = btrfs_truncate_inode_items(trans,
  4203. log, &inode->vfs_inode, 0, 0);
  4204. if (ret != -EAGAIN)
  4205. break;
  4206. }
  4207. }
  4208. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4209. &inode->runtime_flags) ||
  4210. inode_only == LOG_INODE_EXISTS) {
  4211. if (inode_only == LOG_INODE_ALL)
  4212. fast_search = true;
  4213. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4214. ret = drop_objectid_items(trans, log, path, ino,
  4215. max_key.type);
  4216. } else {
  4217. if (inode_only == LOG_INODE_ALL)
  4218. fast_search = true;
  4219. goto log_extents;
  4220. }
  4221. }
  4222. if (ret) {
  4223. err = ret;
  4224. goto out_unlock;
  4225. }
  4226. while (1) {
  4227. ins_nr = 0;
  4228. ret = btrfs_search_forward(root, &min_key,
  4229. path, trans->transid);
  4230. if (ret < 0) {
  4231. err = ret;
  4232. goto out_unlock;
  4233. }
  4234. if (ret != 0)
  4235. break;
  4236. again:
  4237. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  4238. if (min_key.objectid != ino)
  4239. break;
  4240. if (min_key.type > max_key.type)
  4241. break;
  4242. if (min_key.type == BTRFS_INODE_ITEM_KEY)
  4243. need_log_inode_item = false;
  4244. if ((min_key.type == BTRFS_INODE_REF_KEY ||
  4245. min_key.type == BTRFS_INODE_EXTREF_KEY) &&
  4246. inode->generation == trans->transid) {
  4247. u64 other_ino = 0;
  4248. ret = btrfs_check_ref_name_override(path->nodes[0],
  4249. path->slots[0], &min_key, inode,
  4250. &other_ino);
  4251. if (ret < 0) {
  4252. err = ret;
  4253. goto out_unlock;
  4254. } else if (ret > 0 && ctx &&
  4255. other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
  4256. struct btrfs_key inode_key;
  4257. struct inode *other_inode;
  4258. if (ins_nr > 0) {
  4259. ins_nr++;
  4260. } else {
  4261. ins_nr = 1;
  4262. ins_start_slot = path->slots[0];
  4263. }
  4264. ret = copy_items(trans, inode, dst_path, path,
  4265. &last_extent, ins_start_slot,
  4266. ins_nr, inode_only,
  4267. logged_isize);
  4268. if (ret < 0) {
  4269. err = ret;
  4270. goto out_unlock;
  4271. }
  4272. ins_nr = 0;
  4273. btrfs_release_path(path);
  4274. inode_key.objectid = other_ino;
  4275. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4276. inode_key.offset = 0;
  4277. other_inode = btrfs_iget(fs_info->sb,
  4278. &inode_key, root,
  4279. NULL);
  4280. /*
  4281. * If the other inode that had a conflicting dir
  4282. * entry was deleted in the current transaction,
  4283. * we don't need to do more work nor fallback to
  4284. * a transaction commit.
  4285. */
  4286. if (IS_ERR(other_inode) &&
  4287. PTR_ERR(other_inode) == -ENOENT) {
  4288. goto next_key;
  4289. } else if (IS_ERR(other_inode)) {
  4290. err = PTR_ERR(other_inode);
  4291. goto out_unlock;
  4292. }
  4293. /*
  4294. * We are safe logging the other inode without
  4295. * acquiring its i_mutex as long as we log with
  4296. * the LOG_INODE_EXISTS mode. We're safe against
  4297. * concurrent renames of the other inode as well
  4298. * because during a rename we pin the log and
  4299. * update the log with the new name before we
  4300. * unpin it.
  4301. */
  4302. err = btrfs_log_inode(trans, root,
  4303. BTRFS_I(other_inode),
  4304. LOG_OTHER_INODE, 0, LLONG_MAX,
  4305. ctx);
  4306. iput(other_inode);
  4307. if (err)
  4308. goto out_unlock;
  4309. else
  4310. goto next_key;
  4311. }
  4312. }
  4313. /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
  4314. if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
  4315. if (ins_nr == 0)
  4316. goto next_slot;
  4317. ret = copy_items(trans, inode, dst_path, path,
  4318. &last_extent, ins_start_slot,
  4319. ins_nr, inode_only, logged_isize);
  4320. if (ret < 0) {
  4321. err = ret;
  4322. goto out_unlock;
  4323. }
  4324. ins_nr = 0;
  4325. if (ret) {
  4326. btrfs_release_path(path);
  4327. continue;
  4328. }
  4329. goto next_slot;
  4330. }
  4331. src = path->nodes[0];
  4332. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4333. ins_nr++;
  4334. goto next_slot;
  4335. } else if (!ins_nr) {
  4336. ins_start_slot = path->slots[0];
  4337. ins_nr = 1;
  4338. goto next_slot;
  4339. }
  4340. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4341. ins_start_slot, ins_nr, inode_only,
  4342. logged_isize);
  4343. if (ret < 0) {
  4344. err = ret;
  4345. goto out_unlock;
  4346. }
  4347. if (ret) {
  4348. ins_nr = 0;
  4349. btrfs_release_path(path);
  4350. continue;
  4351. }
  4352. ins_nr = 1;
  4353. ins_start_slot = path->slots[0];
  4354. next_slot:
  4355. nritems = btrfs_header_nritems(path->nodes[0]);
  4356. path->slots[0]++;
  4357. if (path->slots[0] < nritems) {
  4358. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  4359. path->slots[0]);
  4360. goto again;
  4361. }
  4362. if (ins_nr) {
  4363. ret = copy_items(trans, inode, dst_path, path,
  4364. &last_extent, ins_start_slot,
  4365. ins_nr, inode_only, logged_isize);
  4366. if (ret < 0) {
  4367. err = ret;
  4368. goto out_unlock;
  4369. }
  4370. ret = 0;
  4371. ins_nr = 0;
  4372. }
  4373. btrfs_release_path(path);
  4374. next_key:
  4375. if (min_key.offset < (u64)-1) {
  4376. min_key.offset++;
  4377. } else if (min_key.type < max_key.type) {
  4378. min_key.type++;
  4379. min_key.offset = 0;
  4380. } else {
  4381. break;
  4382. }
  4383. }
  4384. if (ins_nr) {
  4385. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4386. ins_start_slot, ins_nr, inode_only,
  4387. logged_isize);
  4388. if (ret < 0) {
  4389. err = ret;
  4390. goto out_unlock;
  4391. }
  4392. ret = 0;
  4393. ins_nr = 0;
  4394. }
  4395. btrfs_release_path(path);
  4396. btrfs_release_path(dst_path);
  4397. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4398. if (err)
  4399. goto out_unlock;
  4400. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4401. btrfs_release_path(path);
  4402. btrfs_release_path(dst_path);
  4403. err = btrfs_log_trailing_hole(trans, root, inode, path);
  4404. if (err)
  4405. goto out_unlock;
  4406. }
  4407. log_extents:
  4408. btrfs_release_path(path);
  4409. btrfs_release_path(dst_path);
  4410. if (need_log_inode_item) {
  4411. err = log_inode_item(trans, log, dst_path, inode);
  4412. if (err)
  4413. goto out_unlock;
  4414. }
  4415. if (fast_search) {
  4416. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4417. &logged_list, ctx, start, end);
  4418. if (ret) {
  4419. err = ret;
  4420. goto out_unlock;
  4421. }
  4422. } else if (inode_only == LOG_INODE_ALL) {
  4423. struct extent_map *em, *n;
  4424. write_lock(&em_tree->lock);
  4425. /*
  4426. * We can't just remove every em if we're called for a ranged
  4427. * fsync - that is, one that doesn't cover the whole possible
  4428. * file range (0 to LLONG_MAX). This is because we can have
  4429. * em's that fall outside the range we're logging and therefore
  4430. * their ordered operations haven't completed yet
  4431. * (btrfs_finish_ordered_io() not invoked yet). This means we
  4432. * didn't get their respective file extent item in the fs/subvol
  4433. * tree yet, and need to let the next fast fsync (one which
  4434. * consults the list of modified extent maps) find the em so
  4435. * that it logs a matching file extent item and waits for the
  4436. * respective ordered operation to complete (if it's still
  4437. * running).
  4438. *
  4439. * Removing every em outside the range we're logging would make
  4440. * the next fast fsync not log their matching file extent items,
  4441. * therefore making us lose data after a log replay.
  4442. */
  4443. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  4444. list) {
  4445. const u64 mod_end = em->mod_start + em->mod_len - 1;
  4446. if (em->mod_start >= start && mod_end <= end)
  4447. list_del_init(&em->list);
  4448. }
  4449. write_unlock(&em_tree->lock);
  4450. }
  4451. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
  4452. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4453. ctx);
  4454. if (ret) {
  4455. err = ret;
  4456. goto out_unlock;
  4457. }
  4458. }
  4459. spin_lock(&inode->lock);
  4460. inode->logged_trans = trans->transid;
  4461. inode->last_log_commit = inode->last_sub_trans;
  4462. spin_unlock(&inode->lock);
  4463. out_unlock:
  4464. if (unlikely(err))
  4465. btrfs_put_logged_extents(&logged_list);
  4466. else
  4467. btrfs_submit_logged_extents(&logged_list, log);
  4468. mutex_unlock(&inode->log_mutex);
  4469. btrfs_free_path(path);
  4470. btrfs_free_path(dst_path);
  4471. return err;
  4472. }
  4473. /*
  4474. * Check if we must fallback to a transaction commit when logging an inode.
  4475. * This must be called after logging the inode and is used only in the context
  4476. * when fsyncing an inode requires the need to log some other inode - in which
  4477. * case we can't lock the i_mutex of each other inode we need to log as that
  4478. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4479. * log inodes up or down in the hierarchy) or rename operations for example. So
  4480. * we take the log_mutex of the inode after we have logged it and then check for
  4481. * its last_unlink_trans value - this is safe because any task setting
  4482. * last_unlink_trans must take the log_mutex and it must do this before it does
  4483. * the actual unlink operation, so if we do this check before a concurrent task
  4484. * sets last_unlink_trans it means we've logged a consistent version/state of
  4485. * all the inode items, otherwise we are not sure and must do a transaction
  4486. * commit (the concurrent task might have only updated last_unlink_trans before
  4487. * we logged the inode or it might have also done the unlink).
  4488. */
  4489. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4490. struct btrfs_inode *inode)
  4491. {
  4492. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4493. bool ret = false;
  4494. mutex_lock(&inode->log_mutex);
  4495. if (inode->last_unlink_trans > fs_info->last_trans_committed) {
  4496. /*
  4497. * Make sure any commits to the log are forced to be full
  4498. * commits.
  4499. */
  4500. btrfs_set_log_full_commit(fs_info, trans);
  4501. ret = true;
  4502. }
  4503. mutex_unlock(&inode->log_mutex);
  4504. return ret;
  4505. }
  4506. /*
  4507. * follow the dentry parent pointers up the chain and see if any
  4508. * of the directories in it require a full commit before they can
  4509. * be logged. Returns zero if nothing special needs to be done or 1 if
  4510. * a full commit is required.
  4511. */
  4512. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4513. struct inode *inode,
  4514. struct dentry *parent,
  4515. struct super_block *sb,
  4516. u64 last_committed)
  4517. {
  4518. int ret = 0;
  4519. struct dentry *old_parent = NULL;
  4520. struct inode *orig_inode = inode;
  4521. /*
  4522. * for regular files, if its inode is already on disk, we don't
  4523. * have to worry about the parents at all. This is because
  4524. * we can use the last_unlink_trans field to record renames
  4525. * and other fun in this file.
  4526. */
  4527. if (S_ISREG(inode->i_mode) &&
  4528. BTRFS_I(inode)->generation <= last_committed &&
  4529. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  4530. goto out;
  4531. if (!S_ISDIR(inode->i_mode)) {
  4532. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4533. goto out;
  4534. inode = d_inode(parent);
  4535. }
  4536. while (1) {
  4537. /*
  4538. * If we are logging a directory then we start with our inode,
  4539. * not our parent's inode, so we need to skip setting the
  4540. * logged_trans so that further down in the log code we don't
  4541. * think this inode has already been logged.
  4542. */
  4543. if (inode != orig_inode)
  4544. BTRFS_I(inode)->logged_trans = trans->transid;
  4545. smp_mb();
  4546. if (btrfs_must_commit_transaction(trans, BTRFS_I(inode))) {
  4547. ret = 1;
  4548. break;
  4549. }
  4550. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4551. break;
  4552. if (IS_ROOT(parent)) {
  4553. inode = d_inode(parent);
  4554. if (btrfs_must_commit_transaction(trans, BTRFS_I(inode)))
  4555. ret = 1;
  4556. break;
  4557. }
  4558. parent = dget_parent(parent);
  4559. dput(old_parent);
  4560. old_parent = parent;
  4561. inode = d_inode(parent);
  4562. }
  4563. dput(old_parent);
  4564. out:
  4565. return ret;
  4566. }
  4567. struct btrfs_dir_list {
  4568. u64 ino;
  4569. struct list_head list;
  4570. };
  4571. /*
  4572. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  4573. * details about the why it is needed.
  4574. * This is a recursive operation - if an existing dentry corresponds to a
  4575. * directory, that directory's new entries are logged too (same behaviour as
  4576. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4577. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  4578. * complains about the following circular lock dependency / possible deadlock:
  4579. *
  4580. * CPU0 CPU1
  4581. * ---- ----
  4582. * lock(&type->i_mutex_dir_key#3/2);
  4583. * lock(sb_internal#2);
  4584. * lock(&type->i_mutex_dir_key#3/2);
  4585. * lock(&sb->s_type->i_mutex_key#14);
  4586. *
  4587. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4588. * sb_start_intwrite() in btrfs_start_transaction().
  4589. * Not locking i_mutex of the inodes is still safe because:
  4590. *
  4591. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4592. * that while logging the inode new references (names) are added or removed
  4593. * from the inode, leaving the logged inode item with a link count that does
  4594. * not match the number of logged inode reference items. This is fine because
  4595. * at log replay time we compute the real number of links and correct the
  4596. * link count in the inode item (see replay_one_buffer() and
  4597. * link_to_fixup_dir());
  4598. *
  4599. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4600. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  4601. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  4602. * has a size that doesn't match the sum of the lengths of all the logged
  4603. * names. This does not result in a problem because if a dir_item key is
  4604. * logged but its matching dir_index key is not logged, at log replay time we
  4605. * don't use it to replay the respective name (see replay_one_name()). On the
  4606. * other hand if only the dir_index key ends up being logged, the respective
  4607. * name is added to the fs/subvol tree with both the dir_item and dir_index
  4608. * keys created (see replay_one_name()).
  4609. * The directory's inode item with a wrong i_size is not a problem as well,
  4610. * since we don't use it at log replay time to set the i_size in the inode
  4611. * item of the fs/subvol tree (see overwrite_item()).
  4612. */
  4613. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4614. struct btrfs_root *root,
  4615. struct btrfs_inode *start_inode,
  4616. struct btrfs_log_ctx *ctx)
  4617. {
  4618. struct btrfs_fs_info *fs_info = root->fs_info;
  4619. struct btrfs_root *log = root->log_root;
  4620. struct btrfs_path *path;
  4621. LIST_HEAD(dir_list);
  4622. struct btrfs_dir_list *dir_elem;
  4623. int ret = 0;
  4624. path = btrfs_alloc_path();
  4625. if (!path)
  4626. return -ENOMEM;
  4627. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4628. if (!dir_elem) {
  4629. btrfs_free_path(path);
  4630. return -ENOMEM;
  4631. }
  4632. dir_elem->ino = btrfs_ino(start_inode);
  4633. list_add_tail(&dir_elem->list, &dir_list);
  4634. while (!list_empty(&dir_list)) {
  4635. struct extent_buffer *leaf;
  4636. struct btrfs_key min_key;
  4637. int nritems;
  4638. int i;
  4639. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  4640. list);
  4641. if (ret)
  4642. goto next_dir_inode;
  4643. min_key.objectid = dir_elem->ino;
  4644. min_key.type = BTRFS_DIR_ITEM_KEY;
  4645. min_key.offset = 0;
  4646. again:
  4647. btrfs_release_path(path);
  4648. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  4649. if (ret < 0) {
  4650. goto next_dir_inode;
  4651. } else if (ret > 0) {
  4652. ret = 0;
  4653. goto next_dir_inode;
  4654. }
  4655. process_leaf:
  4656. leaf = path->nodes[0];
  4657. nritems = btrfs_header_nritems(leaf);
  4658. for (i = path->slots[0]; i < nritems; i++) {
  4659. struct btrfs_dir_item *di;
  4660. struct btrfs_key di_key;
  4661. struct inode *di_inode;
  4662. struct btrfs_dir_list *new_dir_elem;
  4663. int log_mode = LOG_INODE_EXISTS;
  4664. int type;
  4665. btrfs_item_key_to_cpu(leaf, &min_key, i);
  4666. if (min_key.objectid != dir_elem->ino ||
  4667. min_key.type != BTRFS_DIR_ITEM_KEY)
  4668. goto next_dir_inode;
  4669. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  4670. type = btrfs_dir_type(leaf, di);
  4671. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  4672. type != BTRFS_FT_DIR)
  4673. continue;
  4674. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4675. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4676. continue;
  4677. btrfs_release_path(path);
  4678. di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
  4679. if (IS_ERR(di_inode)) {
  4680. ret = PTR_ERR(di_inode);
  4681. goto next_dir_inode;
  4682. }
  4683. if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
  4684. iput(di_inode);
  4685. break;
  4686. }
  4687. ctx->log_new_dentries = false;
  4688. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  4689. log_mode = LOG_INODE_ALL;
  4690. ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
  4691. log_mode, 0, LLONG_MAX, ctx);
  4692. if (!ret &&
  4693. btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
  4694. ret = 1;
  4695. iput(di_inode);
  4696. if (ret)
  4697. goto next_dir_inode;
  4698. if (ctx->log_new_dentries) {
  4699. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  4700. GFP_NOFS);
  4701. if (!new_dir_elem) {
  4702. ret = -ENOMEM;
  4703. goto next_dir_inode;
  4704. }
  4705. new_dir_elem->ino = di_key.objectid;
  4706. list_add_tail(&new_dir_elem->list, &dir_list);
  4707. }
  4708. break;
  4709. }
  4710. if (i == nritems) {
  4711. ret = btrfs_next_leaf(log, path);
  4712. if (ret < 0) {
  4713. goto next_dir_inode;
  4714. } else if (ret > 0) {
  4715. ret = 0;
  4716. goto next_dir_inode;
  4717. }
  4718. goto process_leaf;
  4719. }
  4720. if (min_key.offset < (u64)-1) {
  4721. min_key.offset++;
  4722. goto again;
  4723. }
  4724. next_dir_inode:
  4725. list_del(&dir_elem->list);
  4726. kfree(dir_elem);
  4727. }
  4728. btrfs_free_path(path);
  4729. return ret;
  4730. }
  4731. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  4732. struct inode *inode,
  4733. struct btrfs_log_ctx *ctx)
  4734. {
  4735. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4736. int ret;
  4737. struct btrfs_path *path;
  4738. struct btrfs_key key;
  4739. struct btrfs_root *root = BTRFS_I(inode)->root;
  4740. const u64 ino = btrfs_ino(BTRFS_I(inode));
  4741. path = btrfs_alloc_path();
  4742. if (!path)
  4743. return -ENOMEM;
  4744. path->skip_locking = 1;
  4745. path->search_commit_root = 1;
  4746. key.objectid = ino;
  4747. key.type = BTRFS_INODE_REF_KEY;
  4748. key.offset = 0;
  4749. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4750. if (ret < 0)
  4751. goto out;
  4752. while (true) {
  4753. struct extent_buffer *leaf = path->nodes[0];
  4754. int slot = path->slots[0];
  4755. u32 cur_offset = 0;
  4756. u32 item_size;
  4757. unsigned long ptr;
  4758. if (slot >= btrfs_header_nritems(leaf)) {
  4759. ret = btrfs_next_leaf(root, path);
  4760. if (ret < 0)
  4761. goto out;
  4762. else if (ret > 0)
  4763. break;
  4764. continue;
  4765. }
  4766. btrfs_item_key_to_cpu(leaf, &key, slot);
  4767. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  4768. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  4769. break;
  4770. item_size = btrfs_item_size_nr(leaf, slot);
  4771. ptr = btrfs_item_ptr_offset(leaf, slot);
  4772. while (cur_offset < item_size) {
  4773. struct btrfs_key inode_key;
  4774. struct inode *dir_inode;
  4775. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4776. inode_key.offset = 0;
  4777. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  4778. struct btrfs_inode_extref *extref;
  4779. extref = (struct btrfs_inode_extref *)
  4780. (ptr + cur_offset);
  4781. inode_key.objectid = btrfs_inode_extref_parent(
  4782. leaf, extref);
  4783. cur_offset += sizeof(*extref);
  4784. cur_offset += btrfs_inode_extref_name_len(leaf,
  4785. extref);
  4786. } else {
  4787. inode_key.objectid = key.offset;
  4788. cur_offset = item_size;
  4789. }
  4790. dir_inode = btrfs_iget(fs_info->sb, &inode_key,
  4791. root, NULL);
  4792. /* If parent inode was deleted, skip it. */
  4793. if (IS_ERR(dir_inode))
  4794. continue;
  4795. if (ctx)
  4796. ctx->log_new_dentries = false;
  4797. ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
  4798. LOG_INODE_ALL, 0, LLONG_MAX, ctx);
  4799. if (!ret &&
  4800. btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
  4801. ret = 1;
  4802. if (!ret && ctx && ctx->log_new_dentries)
  4803. ret = log_new_dir_dentries(trans, root,
  4804. BTRFS_I(dir_inode), ctx);
  4805. iput(dir_inode);
  4806. if (ret)
  4807. goto out;
  4808. }
  4809. path->slots[0]++;
  4810. }
  4811. ret = 0;
  4812. out:
  4813. btrfs_free_path(path);
  4814. return ret;
  4815. }
  4816. /*
  4817. * helper function around btrfs_log_inode to make sure newly created
  4818. * parent directories also end up in the log. A minimal inode and backref
  4819. * only logging is done of any parent directories that are older than
  4820. * the last committed transaction
  4821. */
  4822. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  4823. struct btrfs_root *root, struct inode *inode,
  4824. struct dentry *parent,
  4825. const loff_t start,
  4826. const loff_t end,
  4827. int exists_only,
  4828. struct btrfs_log_ctx *ctx)
  4829. {
  4830. struct btrfs_fs_info *fs_info = root->fs_info;
  4831. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  4832. struct super_block *sb;
  4833. struct dentry *old_parent = NULL;
  4834. int ret = 0;
  4835. u64 last_committed = fs_info->last_trans_committed;
  4836. bool log_dentries = false;
  4837. struct inode *orig_inode = inode;
  4838. sb = inode->i_sb;
  4839. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  4840. ret = 1;
  4841. goto end_no_trans;
  4842. }
  4843. /*
  4844. * The prev transaction commit doesn't complete, we need do
  4845. * full commit by ourselves.
  4846. */
  4847. if (fs_info->last_trans_log_full_commit >
  4848. fs_info->last_trans_committed) {
  4849. ret = 1;
  4850. goto end_no_trans;
  4851. }
  4852. if (root != BTRFS_I(inode)->root ||
  4853. btrfs_root_refs(&root->root_item) == 0) {
  4854. ret = 1;
  4855. goto end_no_trans;
  4856. }
  4857. ret = check_parent_dirs_for_sync(trans, inode, parent,
  4858. sb, last_committed);
  4859. if (ret)
  4860. goto end_no_trans;
  4861. if (btrfs_inode_in_log(BTRFS_I(inode), trans->transid)) {
  4862. ret = BTRFS_NO_LOG_SYNC;
  4863. goto end_no_trans;
  4864. }
  4865. ret = start_log_trans(trans, root, ctx);
  4866. if (ret)
  4867. goto end_no_trans;
  4868. ret = btrfs_log_inode(trans, root, BTRFS_I(inode), inode_only,
  4869. start, end, ctx);
  4870. if (ret)
  4871. goto end_trans;
  4872. /*
  4873. * for regular files, if its inode is already on disk, we don't
  4874. * have to worry about the parents at all. This is because
  4875. * we can use the last_unlink_trans field to record renames
  4876. * and other fun in this file.
  4877. */
  4878. if (S_ISREG(inode->i_mode) &&
  4879. BTRFS_I(inode)->generation <= last_committed &&
  4880. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  4881. ret = 0;
  4882. goto end_trans;
  4883. }
  4884. if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
  4885. log_dentries = true;
  4886. /*
  4887. * On unlink we must make sure all our current and old parent directory
  4888. * inodes are fully logged. This is to prevent leaving dangling
  4889. * directory index entries in directories that were our parents but are
  4890. * not anymore. Not doing this results in old parent directory being
  4891. * impossible to delete after log replay (rmdir will always fail with
  4892. * error -ENOTEMPTY).
  4893. *
  4894. * Example 1:
  4895. *
  4896. * mkdir testdir
  4897. * touch testdir/foo
  4898. * ln testdir/foo testdir/bar
  4899. * sync
  4900. * unlink testdir/bar
  4901. * xfs_io -c fsync testdir/foo
  4902. * <power failure>
  4903. * mount fs, triggers log replay
  4904. *
  4905. * If we don't log the parent directory (testdir), after log replay the
  4906. * directory still has an entry pointing to the file inode using the bar
  4907. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  4908. * the file inode has a link count of 1.
  4909. *
  4910. * Example 2:
  4911. *
  4912. * mkdir testdir
  4913. * touch foo
  4914. * ln foo testdir/foo2
  4915. * ln foo testdir/foo3
  4916. * sync
  4917. * unlink testdir/foo3
  4918. * xfs_io -c fsync foo
  4919. * <power failure>
  4920. * mount fs, triggers log replay
  4921. *
  4922. * Similar as the first example, after log replay the parent directory
  4923. * testdir still has an entry pointing to the inode file with name foo3
  4924. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  4925. * and has a link count of 2.
  4926. */
  4927. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  4928. ret = btrfs_log_all_parents(trans, orig_inode, ctx);
  4929. if (ret)
  4930. goto end_trans;
  4931. }
  4932. while (1) {
  4933. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4934. break;
  4935. inode = d_inode(parent);
  4936. if (root != BTRFS_I(inode)->root)
  4937. break;
  4938. if (BTRFS_I(inode)->generation > last_committed) {
  4939. ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
  4940. LOG_INODE_EXISTS,
  4941. 0, LLONG_MAX, ctx);
  4942. if (ret)
  4943. goto end_trans;
  4944. }
  4945. if (IS_ROOT(parent))
  4946. break;
  4947. parent = dget_parent(parent);
  4948. dput(old_parent);
  4949. old_parent = parent;
  4950. }
  4951. if (log_dentries)
  4952. ret = log_new_dir_dentries(trans, root, BTRFS_I(orig_inode), ctx);
  4953. else
  4954. ret = 0;
  4955. end_trans:
  4956. dput(old_parent);
  4957. if (ret < 0) {
  4958. btrfs_set_log_full_commit(fs_info, trans);
  4959. ret = 1;
  4960. }
  4961. if (ret)
  4962. btrfs_remove_log_ctx(root, ctx);
  4963. btrfs_end_log_trans(root);
  4964. end_no_trans:
  4965. return ret;
  4966. }
  4967. /*
  4968. * it is not safe to log dentry if the chunk root has added new
  4969. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  4970. * If this returns 1, you must commit the transaction to safely get your
  4971. * data on disk.
  4972. */
  4973. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  4974. struct btrfs_root *root, struct dentry *dentry,
  4975. const loff_t start,
  4976. const loff_t end,
  4977. struct btrfs_log_ctx *ctx)
  4978. {
  4979. struct dentry *parent = dget_parent(dentry);
  4980. int ret;
  4981. ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
  4982. start, end, 0, ctx);
  4983. dput(parent);
  4984. return ret;
  4985. }
  4986. /*
  4987. * should be called during mount to recover any replay any log trees
  4988. * from the FS
  4989. */
  4990. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  4991. {
  4992. int ret;
  4993. struct btrfs_path *path;
  4994. struct btrfs_trans_handle *trans;
  4995. struct btrfs_key key;
  4996. struct btrfs_key found_key;
  4997. struct btrfs_key tmp_key;
  4998. struct btrfs_root *log;
  4999. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  5000. struct walk_control wc = {
  5001. .process_func = process_one_buffer,
  5002. .stage = 0,
  5003. };
  5004. path = btrfs_alloc_path();
  5005. if (!path)
  5006. return -ENOMEM;
  5007. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5008. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  5009. if (IS_ERR(trans)) {
  5010. ret = PTR_ERR(trans);
  5011. goto error;
  5012. }
  5013. wc.trans = trans;
  5014. wc.pin = 1;
  5015. ret = walk_log_tree(trans, log_root_tree, &wc);
  5016. if (ret) {
  5017. btrfs_handle_fs_error(fs_info, ret,
  5018. "Failed to pin buffers while recovering log root tree.");
  5019. goto error;
  5020. }
  5021. again:
  5022. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  5023. key.offset = (u64)-1;
  5024. key.type = BTRFS_ROOT_ITEM_KEY;
  5025. while (1) {
  5026. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5027. if (ret < 0) {
  5028. btrfs_handle_fs_error(fs_info, ret,
  5029. "Couldn't find tree log root.");
  5030. goto error;
  5031. }
  5032. if (ret > 0) {
  5033. if (path->slots[0] == 0)
  5034. break;
  5035. path->slots[0]--;
  5036. }
  5037. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5038. path->slots[0]);
  5039. btrfs_release_path(path);
  5040. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5041. break;
  5042. log = btrfs_read_fs_root(log_root_tree, &found_key);
  5043. if (IS_ERR(log)) {
  5044. ret = PTR_ERR(log);
  5045. btrfs_handle_fs_error(fs_info, ret,
  5046. "Couldn't read tree log root.");
  5047. goto error;
  5048. }
  5049. tmp_key.objectid = found_key.offset;
  5050. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  5051. tmp_key.offset = (u64)-1;
  5052. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  5053. if (IS_ERR(wc.replay_dest)) {
  5054. ret = PTR_ERR(wc.replay_dest);
  5055. free_extent_buffer(log->node);
  5056. free_extent_buffer(log->commit_root);
  5057. kfree(log);
  5058. btrfs_handle_fs_error(fs_info, ret,
  5059. "Couldn't read target root for tree log recovery.");
  5060. goto error;
  5061. }
  5062. wc.replay_dest->log_root = log;
  5063. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5064. ret = walk_log_tree(trans, log, &wc);
  5065. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5066. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5067. path);
  5068. }
  5069. key.offset = found_key.offset - 1;
  5070. wc.replay_dest->log_root = NULL;
  5071. free_extent_buffer(log->node);
  5072. free_extent_buffer(log->commit_root);
  5073. kfree(log);
  5074. if (ret)
  5075. goto error;
  5076. if (found_key.offset == 0)
  5077. break;
  5078. }
  5079. btrfs_release_path(path);
  5080. /* step one is to pin it all, step two is to replay just inodes */
  5081. if (wc.pin) {
  5082. wc.pin = 0;
  5083. wc.process_func = replay_one_buffer;
  5084. wc.stage = LOG_WALK_REPLAY_INODES;
  5085. goto again;
  5086. }
  5087. /* step three is to replay everything */
  5088. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5089. wc.stage++;
  5090. goto again;
  5091. }
  5092. btrfs_free_path(path);
  5093. /* step 4: commit the transaction, which also unpins the blocks */
  5094. ret = btrfs_commit_transaction(trans);
  5095. if (ret)
  5096. return ret;
  5097. free_extent_buffer(log_root_tree->node);
  5098. log_root_tree->log_root = NULL;
  5099. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5100. kfree(log_root_tree);
  5101. return 0;
  5102. error:
  5103. if (wc.trans)
  5104. btrfs_end_transaction(wc.trans);
  5105. btrfs_free_path(path);
  5106. return ret;
  5107. }
  5108. /*
  5109. * there are some corner cases where we want to force a full
  5110. * commit instead of allowing a directory to be logged.
  5111. *
  5112. * They revolve around files there were unlinked from the directory, and
  5113. * this function updates the parent directory so that a full commit is
  5114. * properly done if it is fsync'd later after the unlinks are done.
  5115. *
  5116. * Must be called before the unlink operations (updates to the subvolume tree,
  5117. * inodes, etc) are done.
  5118. */
  5119. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5120. struct btrfs_inode *dir, struct btrfs_inode *inode,
  5121. int for_rename)
  5122. {
  5123. /*
  5124. * when we're logging a file, if it hasn't been renamed
  5125. * or unlinked, and its inode is fully committed on disk,
  5126. * we don't have to worry about walking up the directory chain
  5127. * to log its parents.
  5128. *
  5129. * So, we use the last_unlink_trans field to put this transid
  5130. * into the file. When the file is logged we check it and
  5131. * don't log the parents if the file is fully on disk.
  5132. */
  5133. mutex_lock(&inode->log_mutex);
  5134. inode->last_unlink_trans = trans->transid;
  5135. mutex_unlock(&inode->log_mutex);
  5136. /*
  5137. * if this directory was already logged any new
  5138. * names for this file/dir will get recorded
  5139. */
  5140. smp_mb();
  5141. if (dir->logged_trans == trans->transid)
  5142. return;
  5143. /*
  5144. * if the inode we're about to unlink was logged,
  5145. * the log will be properly updated for any new names
  5146. */
  5147. if (inode->logged_trans == trans->transid)
  5148. return;
  5149. /*
  5150. * when renaming files across directories, if the directory
  5151. * there we're unlinking from gets fsync'd later on, there's
  5152. * no way to find the destination directory later and fsync it
  5153. * properly. So, we have to be conservative and force commits
  5154. * so the new name gets discovered.
  5155. */
  5156. if (for_rename)
  5157. goto record;
  5158. /* we can safely do the unlink without any special recording */
  5159. return;
  5160. record:
  5161. mutex_lock(&dir->log_mutex);
  5162. dir->last_unlink_trans = trans->transid;
  5163. mutex_unlock(&dir->log_mutex);
  5164. }
  5165. /*
  5166. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5167. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5168. * that after replaying the log tree of the parent directory's root we will not
  5169. * see the snapshot anymore and at log replay time we will not see any log tree
  5170. * corresponding to the deleted snapshot's root, which could lead to replaying
  5171. * it after replaying the log tree of the parent directory (which would replay
  5172. * the snapshot delete operation).
  5173. *
  5174. * Must be called before the actual snapshot destroy operation (updates to the
  5175. * parent root and tree of tree roots trees, etc) are done.
  5176. */
  5177. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5178. struct btrfs_inode *dir)
  5179. {
  5180. mutex_lock(&dir->log_mutex);
  5181. dir->last_unlink_trans = trans->transid;
  5182. mutex_unlock(&dir->log_mutex);
  5183. }
  5184. /*
  5185. * Call this after adding a new name for a file and it will properly
  5186. * update the log to reflect the new name.
  5187. *
  5188. * It will return zero if all goes well, and it will return 1 if a
  5189. * full transaction commit is required.
  5190. */
  5191. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5192. struct btrfs_inode *inode, struct btrfs_inode *old_dir,
  5193. struct dentry *parent)
  5194. {
  5195. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5196. struct btrfs_root *root = inode->root;
  5197. /*
  5198. * this will force the logging code to walk the dentry chain
  5199. * up for the file
  5200. */
  5201. if (S_ISREG(inode->vfs_inode.i_mode))
  5202. inode->last_unlink_trans = trans->transid;
  5203. /*
  5204. * if this inode hasn't been logged and directory we're renaming it
  5205. * from hasn't been logged, we don't need to log it
  5206. */
  5207. if (inode->logged_trans <= fs_info->last_trans_committed &&
  5208. (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
  5209. return 0;
  5210. return btrfs_log_inode_parent(trans, root, &inode->vfs_inode, parent, 0,
  5211. LLONG_MAX, 1, NULL);
  5212. }