tree-log.c 175 KB

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