tree-log.c 236 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137
  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 "backref.h"
  16. #include "compression.h"
  17. #include "qgroup.h"
  18. #include "block-group.h"
  19. #include "space-info.h"
  20. #include "inode-item.h"
  21. #include "fs.h"
  22. #include "accessors.h"
  23. #include "extent-tree.h"
  24. #include "root-tree.h"
  25. #include "dir-item.h"
  26. #include "file-item.h"
  27. #include "file.h"
  28. #include "orphan.h"
  29. #include "print-tree.h"
  30. #include "tree-checker.h"
  31. #include "delayed-inode.h"
  32. #define MAX_CONFLICT_INODES 10
  33. /* magic values for the inode_only field in btrfs_log_inode:
  34. *
  35. * LOG_INODE_ALL means to log everything
  36. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  37. * during log replay
  38. */
  39. enum {
  40. LOG_INODE_ALL,
  41. LOG_INODE_EXISTS,
  42. };
  43. /*
  44. * directory trouble cases
  45. *
  46. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  47. * log, we must force a full commit before doing an fsync of the directory
  48. * where the unlink was done.
  49. * ---> record transid of last unlink/rename per directory
  50. *
  51. * mkdir foo/some_dir
  52. * normal commit
  53. * rename foo/some_dir foo2/some_dir
  54. * mkdir foo/some_dir
  55. * fsync foo/some_dir/some_file
  56. *
  57. * The fsync above will unlink the original some_dir without recording
  58. * it in its new location (foo2). After a crash, some_dir will be gone
  59. * unless the fsync of some_file forces a full commit
  60. *
  61. * 2) we must log any new names for any file or dir that is in the fsync
  62. * log. ---> check inode while renaming/linking.
  63. *
  64. * 2a) we must log any new names for any file or dir during rename
  65. * when the directory they are being removed from was logged.
  66. * ---> check inode and old parent dir during rename
  67. *
  68. * 2a is actually the more important variant. With the extra logging
  69. * a crash might unlink the old name without recreating the new one
  70. *
  71. * 3) after a crash, we must go through any directories with a link count
  72. * of zero and redo the rm -rf
  73. *
  74. * mkdir f1/foo
  75. * normal commit
  76. * rm -rf f1/foo
  77. * fsync(f1)
  78. *
  79. * The directory f1 was fully removed from the FS, but fsync was never
  80. * called on f1, only its parent dir. After a crash the rm -rf must
  81. * be replayed. This must be able to recurse down the entire
  82. * directory tree. The inode link count fixup code takes care of the
  83. * ugly details.
  84. */
  85. /*
  86. * stages for the tree walking. The first
  87. * stage (0) is to only pin down the blocks we find
  88. * the second stage (1) is to make sure that all the inodes
  89. * we find in the log are created in the subvolume.
  90. *
  91. * The last stage is to deal with directories and links and extents
  92. * and all the other fun semantics
  93. */
  94. enum {
  95. LOG_WALK_PIN_ONLY,
  96. LOG_WALK_REPLAY_INODES,
  97. LOG_WALK_REPLAY_DIR_INDEX,
  98. LOG_WALK_REPLAY_ALL,
  99. };
  100. /*
  101. * The walk control struct is used to pass state down the chain when processing
  102. * the log tree. The stage field tells us which part of the log tree processing
  103. * we are currently doing.
  104. */
  105. struct walk_control {
  106. /*
  107. * Signal that we are freeing the metadata extents of a log tree.
  108. * This is used at transaction commit time while freeing a log tree.
  109. */
  110. bool free;
  111. /*
  112. * Signal that we are pinning the metadata extents of a log tree and the
  113. * data extents its leaves point to (if using mixed block groups).
  114. * This happens in the first stage of log replay to ensure that during
  115. * replay, while we are modifying subvolume trees, we don't overwrite
  116. * the metadata extents of log trees.
  117. */
  118. bool pin;
  119. /* What stage of the replay code we're currently in. */
  120. int stage;
  121. /*
  122. * Ignore any items from the inode currently being processed. Needs
  123. * to be set every time we find a BTRFS_INODE_ITEM_KEY.
  124. */
  125. bool ignore_cur_inode;
  126. /*
  127. * The root we are currently replaying to. This is NULL for the replay
  128. * stage LOG_WALK_PIN_ONLY.
  129. */
  130. struct btrfs_root *root;
  131. /* The log tree we are currently processing (not NULL for any stage). */
  132. struct btrfs_root *log;
  133. /* The transaction handle used for replaying all log trees. */
  134. struct btrfs_trans_handle *trans;
  135. /*
  136. * The function that gets used to process blocks we find in the tree.
  137. * Note the extent_buffer might not be up to date when it is passed in,
  138. * and it must be checked or read if you need the data inside it.
  139. */
  140. int (*process_func)(struct extent_buffer *eb,
  141. struct walk_control *wc, u64 gen, int level);
  142. /*
  143. * The following are used only when stage is >= LOG_WALK_REPLAY_INODES
  144. * and by the replay_one_buffer() callback.
  145. */
  146. /* The current log leaf being processed. */
  147. struct extent_buffer *log_leaf;
  148. /* The key being processed of the current log leaf. */
  149. struct btrfs_key log_key;
  150. /* The slot being processed of the current log leaf. */
  151. int log_slot;
  152. /* A path used for searches and modifications to subvolume trees. */
  153. struct btrfs_path *subvol_path;
  154. };
  155. static void do_abort_log_replay(struct walk_control *wc, const char *function,
  156. unsigned int line, int error, const char *fmt, ...)
  157. {
  158. struct btrfs_fs_info *fs_info = wc->trans->fs_info;
  159. struct va_format vaf;
  160. va_list args;
  161. /*
  162. * Do nothing if we already aborted, to avoid dumping leaves again which
  163. * can be verbose. Further more, only the first call is useful since it
  164. * is where we have a problem. Note that we do not use the flag
  165. * BTRFS_FS_STATE_TRANS_ABORTED because log replay calls functions that
  166. * are outside of tree-log.c that can abort transactions (such as
  167. * btrfs_add_link() for example), so if that happens we still want to
  168. * dump all log replay specific information below.
  169. */
  170. if (test_and_set_bit(BTRFS_FS_STATE_LOG_REPLAY_ABORTED, &fs_info->fs_state))
  171. return;
  172. btrfs_abort_transaction(wc->trans, error);
  173. if (wc->subvol_path && wc->subvol_path->nodes[0]) {
  174. btrfs_crit(fs_info,
  175. "subvolume (root %llu) leaf currently being processed:",
  176. btrfs_root_id(wc->root));
  177. btrfs_print_leaf(wc->subvol_path->nodes[0]);
  178. }
  179. if (wc->log_leaf) {
  180. btrfs_crit(fs_info,
  181. "log tree (for root %llu) leaf currently being processed (slot %d key " BTRFS_KEY_FMT "):",
  182. btrfs_root_id(wc->root), wc->log_slot,
  183. BTRFS_KEY_FMT_VALUE(&wc->log_key));
  184. btrfs_print_leaf(wc->log_leaf);
  185. }
  186. va_start(args, fmt);
  187. vaf.fmt = fmt;
  188. vaf.va = &args;
  189. btrfs_crit(fs_info,
  190. "log replay failed in %s:%u for root %llu, stage %d, with error %d: %pV",
  191. function, line, btrfs_root_id(wc->root), wc->stage, error, &vaf);
  192. va_end(args);
  193. }
  194. /*
  195. * Use this for aborting a transaction during log replay while we are down the
  196. * call chain of replay_one_buffer(), so that we get a lot more useful
  197. * information for debugging issues when compared to a plain call to
  198. * btrfs_abort_transaction().
  199. */
  200. #define btrfs_abort_log_replay(wc, error, fmt, args...) \
  201. do_abort_log_replay((wc), __func__, __LINE__, (error), fmt, ##args)
  202. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  203. struct btrfs_inode *inode,
  204. int inode_only,
  205. struct btrfs_log_ctx *ctx);
  206. static int link_to_fixup_dir(struct walk_control *wc, u64 objectid);
  207. static noinline int replay_dir_deletes(struct walk_control *wc,
  208. u64 dirid, bool del_all);
  209. static void wait_log_commit(struct btrfs_root *root, int transid);
  210. /*
  211. * tree logging is a special write ahead log used to make sure that
  212. * fsyncs and O_SYNCs can happen without doing full tree commits.
  213. *
  214. * Full tree commits are expensive because they require commonly
  215. * modified blocks to be recowed, creating many dirty pages in the
  216. * extent tree an 4x-6x higher write load than ext3.
  217. *
  218. * Instead of doing a tree commit on every fsync, we use the
  219. * key ranges and transaction ids to find items for a given file or directory
  220. * that have changed in this transaction. Those items are copied into
  221. * a special tree (one per subvolume root), that tree is written to disk
  222. * and then the fsync is considered complete.
  223. *
  224. * After a crash, items are copied out of the log-tree back into the
  225. * subvolume tree. Any file data extents found are recorded in the extent
  226. * allocation tree, and the log-tree freed.
  227. *
  228. * The log tree is read three times, once to pin down all the extents it is
  229. * using in ram and once, once to create all the inodes logged in the tree
  230. * and once to do all the other items.
  231. */
  232. static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root)
  233. {
  234. unsigned int nofs_flag;
  235. struct btrfs_inode *inode;
  236. /* Only meant to be called for subvolume roots and not for log roots. */
  237. ASSERT(btrfs_is_fstree(btrfs_root_id(root)), "root_id=%llu", btrfs_root_id(root));
  238. /*
  239. * We're holding a transaction handle whether we are logging or
  240. * replaying a log tree, so we must make sure NOFS semantics apply
  241. * because btrfs_alloc_inode() may be triggered and it uses GFP_KERNEL
  242. * to allocate an inode, which can recurse back into the filesystem and
  243. * attempt a transaction commit, resulting in a deadlock.
  244. */
  245. nofs_flag = memalloc_nofs_save();
  246. inode = btrfs_iget(objectid, root);
  247. memalloc_nofs_restore(nofs_flag);
  248. return inode;
  249. }
  250. /*
  251. * start a sub transaction and setup the log tree
  252. * this increments the log tree writer count to make the people
  253. * syncing the tree wait for us to finish
  254. */
  255. static int start_log_trans(struct btrfs_trans_handle *trans,
  256. struct btrfs_root *root,
  257. struct btrfs_log_ctx *ctx)
  258. {
  259. struct btrfs_fs_info *fs_info = root->fs_info;
  260. struct btrfs_root *tree_root = fs_info->tree_root;
  261. const bool zoned = btrfs_is_zoned(fs_info);
  262. int ret = 0;
  263. bool created = false;
  264. /*
  265. * First check if the log root tree was already created. If not, create
  266. * it before locking the root's log_mutex, just to keep lockdep happy.
  267. */
  268. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) {
  269. mutex_lock(&tree_root->log_mutex);
  270. if (!fs_info->log_root_tree) {
  271. ret = btrfs_init_log_root_tree(trans, fs_info);
  272. if (!ret) {
  273. set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state);
  274. created = true;
  275. }
  276. }
  277. mutex_unlock(&tree_root->log_mutex);
  278. if (ret)
  279. return ret;
  280. }
  281. mutex_lock(&root->log_mutex);
  282. again:
  283. if (root->log_root) {
  284. int index = (root->log_transid + 1) % 2;
  285. if (btrfs_need_log_full_commit(trans)) {
  286. ret = BTRFS_LOG_FORCE_COMMIT;
  287. goto out;
  288. }
  289. if (zoned && atomic_read(&root->log_commit[index])) {
  290. wait_log_commit(root, root->log_transid - 1);
  291. goto again;
  292. }
  293. if (!root->log_start_pid) {
  294. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  295. root->log_start_pid = current->pid;
  296. } else if (root->log_start_pid != current->pid) {
  297. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  298. }
  299. } else {
  300. /*
  301. * This means fs_info->log_root_tree was already created
  302. * for some other FS trees. Do the full commit not to mix
  303. * nodes from multiple log transactions to do sequential
  304. * writing.
  305. */
  306. if (zoned && !created) {
  307. ret = BTRFS_LOG_FORCE_COMMIT;
  308. goto out;
  309. }
  310. ret = btrfs_add_log_tree(trans, root);
  311. if (ret)
  312. goto out;
  313. set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
  314. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  315. root->log_start_pid = current->pid;
  316. }
  317. atomic_inc(&root->log_writers);
  318. if (!ctx->logging_new_name) {
  319. int index = root->log_transid % 2;
  320. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  321. ctx->log_transid = root->log_transid;
  322. }
  323. out:
  324. mutex_unlock(&root->log_mutex);
  325. return ret;
  326. }
  327. /*
  328. * returns 0 if there was a log transaction running and we were able
  329. * to join, or returns -ENOENT if there were not transactions
  330. * in progress
  331. */
  332. static int join_running_log_trans(struct btrfs_root *root)
  333. {
  334. const bool zoned = btrfs_is_zoned(root->fs_info);
  335. int ret = -ENOENT;
  336. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
  337. return ret;
  338. mutex_lock(&root->log_mutex);
  339. again:
  340. if (root->log_root) {
  341. int index = (root->log_transid + 1) % 2;
  342. ret = 0;
  343. if (zoned && atomic_read(&root->log_commit[index])) {
  344. wait_log_commit(root, root->log_transid - 1);
  345. goto again;
  346. }
  347. atomic_inc(&root->log_writers);
  348. }
  349. mutex_unlock(&root->log_mutex);
  350. return ret;
  351. }
  352. /*
  353. * This either makes the current running log transaction wait
  354. * until you call btrfs_end_log_trans() or it makes any future
  355. * log transactions wait until you call btrfs_end_log_trans()
  356. */
  357. void btrfs_pin_log_trans(struct btrfs_root *root)
  358. {
  359. atomic_inc(&root->log_writers);
  360. }
  361. /*
  362. * indicate we're done making changes to the log tree
  363. * and wake up anyone waiting to do a sync
  364. */
  365. void btrfs_end_log_trans(struct btrfs_root *root)
  366. {
  367. if (atomic_dec_and_test(&root->log_writers)) {
  368. /* atomic_dec_and_test implies a barrier */
  369. cond_wake_up_nomb(&root->log_writer_wait);
  370. }
  371. }
  372. /*
  373. * process_func used to pin down extents, write them or wait on them
  374. */
  375. static int process_one_buffer(struct extent_buffer *eb,
  376. struct walk_control *wc, u64 gen, int level)
  377. {
  378. struct btrfs_root *log = wc->log;
  379. struct btrfs_trans_handle *trans = wc->trans;
  380. struct btrfs_fs_info *fs_info = log->fs_info;
  381. int ret = 0;
  382. /*
  383. * If this fs is mixed then we need to be able to process the leaves to
  384. * pin down any logged extents, so we have to read the block.
  385. */
  386. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  387. struct btrfs_tree_parent_check check = {
  388. .level = level,
  389. .transid = gen
  390. };
  391. ret = btrfs_read_extent_buffer(eb, &check);
  392. if (unlikely(ret)) {
  393. if (trans)
  394. btrfs_abort_transaction(trans, ret);
  395. else
  396. btrfs_handle_fs_error(fs_info, ret, NULL);
  397. return ret;
  398. }
  399. }
  400. if (wc->pin) {
  401. ASSERT(trans != NULL);
  402. ret = btrfs_pin_extent_for_log_replay(trans, eb);
  403. if (unlikely(ret)) {
  404. btrfs_abort_transaction(trans, ret);
  405. return ret;
  406. }
  407. if (btrfs_buffer_uptodate(eb, gen, false) && level == 0) {
  408. ret = btrfs_exclude_logged_extents(eb);
  409. if (ret)
  410. btrfs_abort_transaction(trans, ret);
  411. }
  412. }
  413. return ret;
  414. }
  415. /*
  416. * Item overwrite used by log replay. The given log tree leaf, slot and key
  417. * from the walk_control structure all refer to the source data we are copying
  418. * out.
  419. *
  420. * The given root is for the tree we are copying into, and path is a scratch
  421. * path for use in this function (it should be released on entry and will be
  422. * released on exit).
  423. *
  424. * If the key is already in the destination tree the existing item is
  425. * overwritten. If the existing item isn't big enough, it is extended.
  426. * If it is too large, it is truncated.
  427. *
  428. * If the key isn't in the destination yet, a new item is inserted.
  429. */
  430. static int overwrite_item(struct walk_control *wc)
  431. {
  432. struct btrfs_trans_handle *trans = wc->trans;
  433. struct btrfs_root *root = wc->root;
  434. int ret;
  435. u32 item_size;
  436. u64 saved_i_size = 0;
  437. int save_old_i_size = 0;
  438. unsigned long src_ptr;
  439. unsigned long dst_ptr;
  440. struct extent_buffer *dst_eb;
  441. int dst_slot;
  442. const bool is_inode_item = (wc->log_key.type == BTRFS_INODE_ITEM_KEY);
  443. /*
  444. * This is only used during log replay, so the root is always from a
  445. * fs/subvolume tree. In case we ever need to support a log root, then
  446. * we'll have to clone the leaf in the path, release the path and use
  447. * the leaf before writing into the log tree. See the comments at
  448. * copy_items() for more details.
  449. */
  450. ASSERT(btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID, "root_id=%llu", btrfs_root_id(root));
  451. item_size = btrfs_item_size(wc->log_leaf, wc->log_slot);
  452. src_ptr = btrfs_item_ptr_offset(wc->log_leaf, wc->log_slot);
  453. /* Look for the key in the destination tree. */
  454. ret = btrfs_search_slot(NULL, root, &wc->log_key, wc->subvol_path, 0, 0);
  455. if (ret < 0) {
  456. btrfs_abort_log_replay(wc, ret,
  457. "failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu",
  458. BTRFS_KEY_FMT_VALUE(&wc->log_key),
  459. btrfs_root_id(root));
  460. return ret;
  461. }
  462. dst_eb = wc->subvol_path->nodes[0];
  463. dst_slot = wc->subvol_path->slots[0];
  464. if (ret == 0) {
  465. char *src_copy;
  466. const u32 dst_size = btrfs_item_size(dst_eb, dst_slot);
  467. if (dst_size != item_size)
  468. goto insert;
  469. if (item_size == 0) {
  470. btrfs_release_path(wc->subvol_path);
  471. return 0;
  472. }
  473. src_copy = kmalloc(item_size, GFP_NOFS);
  474. if (!src_copy) {
  475. btrfs_abort_log_replay(wc, -ENOMEM,
  476. "failed to allocate memory for log leaf item");
  477. return -ENOMEM;
  478. }
  479. read_extent_buffer(wc->log_leaf, src_copy, src_ptr, item_size);
  480. dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot);
  481. ret = memcmp_extent_buffer(dst_eb, src_copy, dst_ptr, item_size);
  482. kfree(src_copy);
  483. /*
  484. * they have the same contents, just return, this saves
  485. * us from cowing blocks in the destination tree and doing
  486. * extra writes that may not have been done by a previous
  487. * sync
  488. */
  489. if (ret == 0) {
  490. btrfs_release_path(wc->subvol_path);
  491. return 0;
  492. }
  493. /*
  494. * We need to load the old nbytes into the inode so when we
  495. * replay the extents we've logged we get the right nbytes.
  496. */
  497. if (is_inode_item) {
  498. struct btrfs_inode_item *item;
  499. u64 nbytes;
  500. u32 mode;
  501. item = btrfs_item_ptr(dst_eb, dst_slot,
  502. struct btrfs_inode_item);
  503. nbytes = btrfs_inode_nbytes(dst_eb, item);
  504. item = btrfs_item_ptr(wc->log_leaf, wc->log_slot,
  505. struct btrfs_inode_item);
  506. btrfs_set_inode_nbytes(wc->log_leaf, item, nbytes);
  507. /*
  508. * If this is a directory we need to reset the i_size to
  509. * 0 so that we can set it up properly when replaying
  510. * the rest of the items in this log.
  511. */
  512. mode = btrfs_inode_mode(wc->log_leaf, item);
  513. if (S_ISDIR(mode))
  514. btrfs_set_inode_size(wc->log_leaf, item, 0);
  515. }
  516. } else if (is_inode_item) {
  517. struct btrfs_inode_item *item;
  518. u32 mode;
  519. /*
  520. * New inode, set nbytes to 0 so that the nbytes comes out
  521. * properly when we replay the extents.
  522. */
  523. item = btrfs_item_ptr(wc->log_leaf, wc->log_slot, struct btrfs_inode_item);
  524. btrfs_set_inode_nbytes(wc->log_leaf, item, 0);
  525. /*
  526. * If this is a directory we need to reset the i_size to 0 so
  527. * that we can set it up properly when replaying the rest of
  528. * the items in this log.
  529. */
  530. mode = btrfs_inode_mode(wc->log_leaf, item);
  531. if (S_ISDIR(mode))
  532. btrfs_set_inode_size(wc->log_leaf, item, 0);
  533. }
  534. insert:
  535. btrfs_release_path(wc->subvol_path);
  536. /* try to insert the key into the destination tree */
  537. wc->subvol_path->skip_release_on_error = true;
  538. ret = btrfs_insert_empty_item(trans, root, wc->subvol_path, &wc->log_key, item_size);
  539. wc->subvol_path->skip_release_on_error = false;
  540. dst_eb = wc->subvol_path->nodes[0];
  541. dst_slot = wc->subvol_path->slots[0];
  542. /* make sure any existing item is the correct size */
  543. if (ret == -EEXIST || ret == -EOVERFLOW) {
  544. const u32 found_size = btrfs_item_size(dst_eb, dst_slot);
  545. if (found_size > item_size)
  546. btrfs_truncate_item(trans, wc->subvol_path, item_size, 1);
  547. else if (found_size < item_size)
  548. btrfs_extend_item(trans, wc->subvol_path, item_size - found_size);
  549. } else if (ret) {
  550. btrfs_abort_log_replay(wc, ret,
  551. "failed to insert item for key " BTRFS_KEY_FMT,
  552. BTRFS_KEY_FMT_VALUE(&wc->log_key));
  553. return ret;
  554. }
  555. dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot);
  556. /* don't overwrite an existing inode if the generation number
  557. * was logged as zero. This is done when the tree logging code
  558. * is just logging an inode to make sure it exists after recovery.
  559. *
  560. * Also, don't overwrite i_size on directories during replay.
  561. * log replay inserts and removes directory items based on the
  562. * state of the tree found in the subvolume, and i_size is modified
  563. * as it goes
  564. */
  565. if (is_inode_item && ret == -EEXIST) {
  566. struct btrfs_inode_item *src_item;
  567. struct btrfs_inode_item *dst_item;
  568. src_item = (struct btrfs_inode_item *)src_ptr;
  569. dst_item = (struct btrfs_inode_item *)dst_ptr;
  570. if (btrfs_inode_generation(wc->log_leaf, src_item) == 0) {
  571. const u64 ino_size = btrfs_inode_size(wc->log_leaf, src_item);
  572. /*
  573. * For regular files an ino_size == 0 is used only when
  574. * logging that an inode exists, as part of a directory
  575. * fsync, and the inode wasn't fsynced before. In this
  576. * case don't set the size of the inode in the fs/subvol
  577. * tree, otherwise we would be throwing valid data away.
  578. */
  579. if (S_ISREG(btrfs_inode_mode(wc->log_leaf, src_item)) &&
  580. S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
  581. ino_size != 0)
  582. btrfs_set_inode_size(dst_eb, dst_item, ino_size);
  583. goto no_copy;
  584. }
  585. if (S_ISDIR(btrfs_inode_mode(wc->log_leaf, src_item)) &&
  586. S_ISDIR(btrfs_inode_mode(dst_eb, dst_item))) {
  587. save_old_i_size = 1;
  588. saved_i_size = btrfs_inode_size(dst_eb, dst_item);
  589. }
  590. }
  591. copy_extent_buffer(dst_eb, wc->log_leaf, dst_ptr, src_ptr, item_size);
  592. if (save_old_i_size) {
  593. struct btrfs_inode_item *dst_item;
  594. dst_item = (struct btrfs_inode_item *)dst_ptr;
  595. btrfs_set_inode_size(dst_eb, dst_item, saved_i_size);
  596. }
  597. /* make sure the generation is filled in */
  598. if (is_inode_item) {
  599. struct btrfs_inode_item *dst_item;
  600. dst_item = (struct btrfs_inode_item *)dst_ptr;
  601. if (btrfs_inode_generation(dst_eb, dst_item) == 0)
  602. btrfs_set_inode_generation(dst_eb, dst_item, trans->transid);
  603. }
  604. no_copy:
  605. btrfs_release_path(wc->subvol_path);
  606. return 0;
  607. }
  608. static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len,
  609. struct fscrypt_str *name)
  610. {
  611. char *buf;
  612. buf = kmalloc(len, GFP_NOFS);
  613. if (!buf)
  614. return -ENOMEM;
  615. read_extent_buffer(eb, buf, (unsigned long)start, len);
  616. name->name = buf;
  617. name->len = len;
  618. return 0;
  619. }
  620. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  621. * subvolume 'root'. path is released on entry and should be released
  622. * on exit.
  623. *
  624. * extents in the log tree have not been allocated out of the extent
  625. * tree yet. So, this completes the allocation, taking a reference
  626. * as required if the extent already exists or creating a new extent
  627. * if it isn't in the extent allocation tree yet.
  628. *
  629. * The extent is inserted into the file, dropping any existing extents
  630. * from the file that overlap the new one.
  631. */
  632. static noinline int replay_one_extent(struct walk_control *wc)
  633. {
  634. struct btrfs_trans_handle *trans = wc->trans;
  635. struct btrfs_root *root = wc->root;
  636. struct btrfs_drop_extents_args drop_args = { 0 };
  637. struct btrfs_fs_info *fs_info = root->fs_info;
  638. int found_type;
  639. u64 extent_end;
  640. const u64 start = wc->log_key.offset;
  641. u64 nbytes = 0;
  642. u64 csum_start;
  643. u64 csum_end;
  644. LIST_HEAD(ordered_sums);
  645. u64 offset;
  646. unsigned long dest_offset;
  647. struct btrfs_key ins;
  648. struct btrfs_file_extent_item *item;
  649. struct btrfs_inode *inode = NULL;
  650. int ret = 0;
  651. item = btrfs_item_ptr(wc->log_leaf, wc->log_slot, struct btrfs_file_extent_item);
  652. found_type = btrfs_file_extent_type(wc->log_leaf, item);
  653. if (found_type == BTRFS_FILE_EXTENT_REG ||
  654. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  655. extent_end = start + btrfs_file_extent_num_bytes(wc->log_leaf, item);
  656. /* Holes don't take up space. */
  657. if (btrfs_file_extent_disk_bytenr(wc->log_leaf, item) != 0)
  658. nbytes = btrfs_file_extent_num_bytes(wc->log_leaf, item);
  659. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  660. nbytes = btrfs_file_extent_ram_bytes(wc->log_leaf, item);
  661. extent_end = ALIGN(start + nbytes, fs_info->sectorsize);
  662. } else {
  663. btrfs_abort_log_replay(wc, -EUCLEAN,
  664. "unexpected extent type=%d root=%llu inode=%llu offset=%llu",
  665. found_type, btrfs_root_id(root),
  666. wc->log_key.objectid, wc->log_key.offset);
  667. return -EUCLEAN;
  668. }
  669. inode = btrfs_iget_logging(wc->log_key.objectid, root);
  670. if (IS_ERR(inode)) {
  671. ret = PTR_ERR(inode);
  672. btrfs_abort_log_replay(wc, ret,
  673. "failed to get inode %llu for root %llu",
  674. wc->log_key.objectid, btrfs_root_id(root));
  675. return ret;
  676. }
  677. /*
  678. * first check to see if we already have this extent in the
  679. * file. This must be done before the btrfs_drop_extents run
  680. * so we don't try to drop this extent.
  681. */
  682. ret = btrfs_lookup_file_extent(trans, root, wc->subvol_path,
  683. btrfs_ino(inode), start, 0);
  684. if (ret == 0 &&
  685. (found_type == BTRFS_FILE_EXTENT_REG ||
  686. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  687. struct extent_buffer *leaf = wc->subvol_path->nodes[0];
  688. struct btrfs_file_extent_item existing;
  689. unsigned long ptr;
  690. ptr = btrfs_item_ptr_offset(leaf, wc->subvol_path->slots[0]);
  691. read_extent_buffer(leaf, &existing, ptr, sizeof(existing));
  692. /*
  693. * we already have a pointer to this exact extent,
  694. * we don't have to do anything
  695. */
  696. if (memcmp_extent_buffer(wc->log_leaf, &existing, (unsigned long)item,
  697. sizeof(existing)) == 0) {
  698. btrfs_release_path(wc->subvol_path);
  699. goto out;
  700. }
  701. }
  702. btrfs_release_path(wc->subvol_path);
  703. /* drop any overlapping extents */
  704. drop_args.start = start;
  705. drop_args.end = extent_end;
  706. drop_args.drop_cache = true;
  707. drop_args.path = wc->subvol_path;
  708. ret = btrfs_drop_extents(trans, root, inode, &drop_args);
  709. if (ret) {
  710. btrfs_abort_log_replay(wc, ret,
  711. "failed to drop extents for inode %llu range [%llu, %llu) root %llu",
  712. wc->log_key.objectid, start, extent_end,
  713. btrfs_root_id(root));
  714. goto out;
  715. }
  716. if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  717. /* inline extents are easy, we just overwrite them */
  718. ret = overwrite_item(wc);
  719. if (ret)
  720. goto out;
  721. goto update_inode;
  722. }
  723. /*
  724. * If not an inline extent, it can only be a regular or prealloc one.
  725. * We have checked that above and returned -EUCLEAN if not.
  726. */
  727. /* A hole and NO_HOLES feature enabled, nothing else to do. */
  728. if (btrfs_file_extent_disk_bytenr(wc->log_leaf, item) == 0 &&
  729. btrfs_fs_incompat(fs_info, NO_HOLES))
  730. goto update_inode;
  731. ret = btrfs_insert_empty_item(trans, root, wc->subvol_path,
  732. &wc->log_key, sizeof(*item));
  733. if (ret) {
  734. btrfs_abort_log_replay(wc, ret,
  735. "failed to insert item with key " BTRFS_KEY_FMT " root %llu",
  736. BTRFS_KEY_FMT_VALUE(&wc->log_key),
  737. btrfs_root_id(root));
  738. goto out;
  739. }
  740. dest_offset = btrfs_item_ptr_offset(wc->subvol_path->nodes[0],
  741. wc->subvol_path->slots[0]);
  742. copy_extent_buffer(wc->subvol_path->nodes[0], wc->log_leaf, dest_offset,
  743. (unsigned long)item, sizeof(*item));
  744. /*
  745. * We have an explicit hole and NO_HOLES is not enabled. We have added
  746. * the hole file extent item to the subvolume tree, so we don't have
  747. * anything else to do other than update the file extent item range and
  748. * update the inode item.
  749. */
  750. if (btrfs_file_extent_disk_bytenr(wc->log_leaf, item) == 0) {
  751. btrfs_release_path(wc->subvol_path);
  752. goto update_inode;
  753. }
  754. ins.objectid = btrfs_file_extent_disk_bytenr(wc->log_leaf, item);
  755. ins.type = BTRFS_EXTENT_ITEM_KEY;
  756. ins.offset = btrfs_file_extent_disk_num_bytes(wc->log_leaf, item);
  757. offset = wc->log_key.offset - btrfs_file_extent_offset(wc->log_leaf, item);
  758. /*
  759. * Manually record dirty extent, as here we did a shallow file extent
  760. * item copy and skip normal backref update, but modifying extent tree
  761. * all by ourselves. So need to manually record dirty extent for qgroup,
  762. * as the owner of the file extent changed from log tree (doesn't affect
  763. * qgroup) to fs/file tree (affects qgroup).
  764. */
  765. ret = btrfs_qgroup_trace_extent(trans, ins.objectid, ins.offset);
  766. if (ret < 0) {
  767. btrfs_abort_log_replay(wc, ret,
  768. "failed to trace extent for bytenr %llu disk_num_bytes %llu inode %llu root %llu",
  769. ins.objectid, ins.offset,
  770. wc->log_key.objectid, btrfs_root_id(root));
  771. goto out;
  772. }
  773. /*
  774. * Is this extent already allocated in the extent tree?
  775. * If so, just add a reference.
  776. */
  777. ret = btrfs_lookup_data_extent(fs_info, ins.objectid, ins.offset);
  778. if (ret < 0) {
  779. btrfs_abort_log_replay(wc, ret,
  780. "failed to lookup data extent for bytenr %llu disk_num_bytes %llu inode %llu root %llu",
  781. ins.objectid, ins.offset,
  782. wc->log_key.objectid, btrfs_root_id(root));
  783. goto out;
  784. } else if (ret == 0) {
  785. struct btrfs_ref ref = {
  786. .action = BTRFS_ADD_DELAYED_REF,
  787. .bytenr = ins.objectid,
  788. .num_bytes = ins.offset,
  789. .owning_root = btrfs_root_id(root),
  790. .ref_root = btrfs_root_id(root),
  791. };
  792. btrfs_init_data_ref(&ref, wc->log_key.objectid, offset, 0, false);
  793. ret = btrfs_inc_extent_ref(trans, &ref);
  794. if (ret) {
  795. btrfs_abort_log_replay(wc, ret,
  796. "failed to increment data extent for bytenr %llu disk_num_bytes %llu inode %llu root %llu",
  797. ins.objectid, ins.offset,
  798. wc->log_key.objectid,
  799. btrfs_root_id(root));
  800. goto out;
  801. }
  802. } else {
  803. /* Insert the extent pointer in the extent tree. */
  804. ret = btrfs_alloc_logged_file_extent(trans, btrfs_root_id(root),
  805. wc->log_key.objectid, offset, &ins);
  806. if (ret) {
  807. btrfs_abort_log_replay(wc, ret,
  808. "failed to allocate logged data extent for bytenr %llu disk_num_bytes %llu offset %llu inode %llu root %llu",
  809. ins.objectid, ins.offset, offset,
  810. wc->log_key.objectid, btrfs_root_id(root));
  811. goto out;
  812. }
  813. }
  814. btrfs_release_path(wc->subvol_path);
  815. if (btrfs_file_extent_compression(wc->log_leaf, item)) {
  816. csum_start = ins.objectid;
  817. csum_end = csum_start + ins.offset;
  818. } else {
  819. csum_start = ins.objectid + btrfs_file_extent_offset(wc->log_leaf, item);
  820. csum_end = csum_start + btrfs_file_extent_num_bytes(wc->log_leaf, item);
  821. }
  822. ret = btrfs_lookup_csums_list(root->log_root, csum_start, csum_end - 1,
  823. &ordered_sums, false);
  824. if (ret < 0) {
  825. btrfs_abort_log_replay(wc, ret,
  826. "failed to lookups csums for range [%llu, %llu) inode %llu root %llu",
  827. csum_start, csum_end, wc->log_key.objectid,
  828. btrfs_root_id(root));
  829. goto out;
  830. }
  831. ret = 0;
  832. /*
  833. * Now delete all existing cums in the csum root that cover our range.
  834. * We do this because we can have an extent that is completely
  835. * referenced by one file extent item and partially referenced by
  836. * another file extent item (like after using the clone or extent_same
  837. * ioctls). In this case if we end up doing the replay of the one that
  838. * partially references the extent first, and we do not do the csum
  839. * deletion below, we can get 2 csum items in the csum tree that overlap
  840. * each other. For example, imagine our log has the two following file
  841. * extent items:
  842. *
  843. * key (257 EXTENT_DATA 409600)
  844. * extent data disk byte 12845056 nr 102400
  845. * extent data offset 20480 nr 20480 ram 102400
  846. *
  847. * key (257 EXTENT_DATA 819200)
  848. * extent data disk byte 12845056 nr 102400
  849. * extent data offset 0 nr 102400 ram 102400
  850. *
  851. * Where the second one fully references the 100K extent that starts at
  852. * disk byte 12845056, and the log tree has a single csum item that
  853. * covers the entire range of the extent:
  854. *
  855. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  856. *
  857. * After the first file extent item is replayed, the csum tree gets the
  858. * following csum item:
  859. *
  860. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  861. *
  862. * Which covers the 20K sub-range starting at offset 20K of our extent.
  863. * Now when we replay the second file extent item, if we do not delete
  864. * existing csum items that cover any of its blocks, we end up getting
  865. * two csum items in our csum tree that overlap each other:
  866. *
  867. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  868. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  869. *
  870. * Which is a problem, because after this anyone trying to lookup for
  871. * the checksum of any block of our extent starting at an offset of 40K
  872. * or higher, will end up looking at the second csum item only, which
  873. * does not contain the checksum for any block starting at offset 40K or
  874. * higher of our extent.
  875. */
  876. while (!list_empty(&ordered_sums)) {
  877. struct btrfs_ordered_sum *sums;
  878. struct btrfs_root *csum_root;
  879. sums = list_first_entry(&ordered_sums, struct btrfs_ordered_sum, list);
  880. csum_root = btrfs_csum_root(fs_info, sums->logical);
  881. if (unlikely(!csum_root)) {
  882. btrfs_err(fs_info,
  883. "missing csum root for extent at bytenr %llu",
  884. sums->logical);
  885. ret = -EUCLEAN;
  886. }
  887. if (!ret) {
  888. ret = btrfs_del_csums(trans, csum_root, sums->logical,
  889. sums->len);
  890. if (ret)
  891. btrfs_abort_log_replay(wc, ret,
  892. "failed to delete csums for range [%llu, %llu) inode %llu root %llu",
  893. sums->logical,
  894. sums->logical + sums->len,
  895. wc->log_key.objectid,
  896. btrfs_root_id(root));
  897. }
  898. if (!ret) {
  899. ret = btrfs_csum_file_blocks(trans, csum_root, sums);
  900. if (ret)
  901. btrfs_abort_log_replay(wc, ret,
  902. "failed to add csums for range [%llu, %llu) inode %llu root %llu",
  903. sums->logical,
  904. sums->logical + sums->len,
  905. wc->log_key.objectid,
  906. btrfs_root_id(root));
  907. }
  908. list_del(&sums->list);
  909. kfree(sums);
  910. }
  911. if (ret)
  912. goto out;
  913. update_inode:
  914. ret = btrfs_inode_set_file_extent_range(inode, start, extent_end - start);
  915. if (ret) {
  916. btrfs_abort_log_replay(wc, ret,
  917. "failed to set file extent range [%llu, %llu) inode %llu root %llu",
  918. start, extent_end, wc->log_key.objectid,
  919. btrfs_root_id(root));
  920. goto out;
  921. }
  922. btrfs_update_inode_bytes(inode, nbytes, drop_args.bytes_found);
  923. ret = btrfs_update_inode(trans, inode);
  924. if (ret)
  925. btrfs_abort_log_replay(wc, ret,
  926. "failed to update inode %llu root %llu",
  927. wc->log_key.objectid, btrfs_root_id(root));
  928. out:
  929. iput(&inode->vfs_inode);
  930. return ret;
  931. }
  932. static int unlink_inode_for_log_replay(struct walk_control *wc,
  933. struct btrfs_inode *dir,
  934. struct btrfs_inode *inode,
  935. const struct fscrypt_str *name)
  936. {
  937. struct btrfs_trans_handle *trans = wc->trans;
  938. int ret;
  939. ret = btrfs_unlink_inode(trans, dir, inode, name);
  940. if (ret) {
  941. btrfs_abort_log_replay(wc, ret,
  942. "failed to unlink inode %llu parent dir %llu name %.*s root %llu",
  943. btrfs_ino(inode), btrfs_ino(dir), name->len,
  944. name->name, btrfs_root_id(inode->root));
  945. return ret;
  946. }
  947. /*
  948. * Whenever we need to check if a name exists or not, we check the
  949. * fs/subvolume tree. So after an unlink we must run delayed items, so
  950. * that future checks for a name during log replay see that the name
  951. * does not exists anymore.
  952. */
  953. ret = btrfs_run_delayed_items(trans);
  954. if (ret)
  955. btrfs_abort_log_replay(wc, ret,
  956. "failed to run delayed items current inode %llu parent dir %llu name %.*s root %llu",
  957. btrfs_ino(inode), btrfs_ino(dir), name->len,
  958. name->name, btrfs_root_id(inode->root));
  959. return ret;
  960. }
  961. /*
  962. * when cleaning up conflicts between the directory names in the
  963. * subvolume, directory names in the log and directory names in the
  964. * inode back references, we may have to unlink inodes from directories.
  965. *
  966. * This is a helper function to do the unlink of a specific directory
  967. * item
  968. */
  969. static noinline int drop_one_dir_item(struct walk_control *wc,
  970. struct btrfs_inode *dir,
  971. struct btrfs_dir_item *di)
  972. {
  973. struct btrfs_root *root = dir->root;
  974. struct btrfs_inode *inode;
  975. struct fscrypt_str name;
  976. struct extent_buffer *leaf = wc->subvol_path->nodes[0];
  977. struct btrfs_key location;
  978. int ret;
  979. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  980. ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name);
  981. if (ret) {
  982. btrfs_abort_log_replay(wc, ret,
  983. "failed to allocate name for dir %llu root %llu",
  984. btrfs_ino(dir), btrfs_root_id(root));
  985. return ret;
  986. }
  987. btrfs_release_path(wc->subvol_path);
  988. inode = btrfs_iget_logging(location.objectid, root);
  989. if (IS_ERR(inode)) {
  990. ret = PTR_ERR(inode);
  991. btrfs_abort_log_replay(wc, ret,
  992. "failed to open inode %llu parent dir %llu name %.*s root %llu",
  993. location.objectid, btrfs_ino(dir),
  994. name.len, name.name, btrfs_root_id(root));
  995. inode = NULL;
  996. goto out;
  997. }
  998. ret = link_to_fixup_dir(wc, location.objectid);
  999. if (ret)
  1000. goto out;
  1001. ret = unlink_inode_for_log_replay(wc, dir, inode, &name);
  1002. out:
  1003. kfree(name.name);
  1004. if (inode)
  1005. iput(&inode->vfs_inode);
  1006. return ret;
  1007. }
  1008. /*
  1009. * See if a given name and sequence number found in an inode back reference are
  1010. * already in a directory and correctly point to this inode.
  1011. *
  1012. * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
  1013. * exists.
  1014. */
  1015. static noinline int inode_in_dir(struct btrfs_root *root,
  1016. struct btrfs_path *path,
  1017. u64 dirid, u64 objectid, u64 index,
  1018. struct fscrypt_str *name)
  1019. {
  1020. struct btrfs_dir_item *di;
  1021. struct btrfs_key location;
  1022. int ret = 0;
  1023. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  1024. index, name, 0);
  1025. if (IS_ERR(di)) {
  1026. ret = PTR_ERR(di);
  1027. goto out;
  1028. } else if (di) {
  1029. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  1030. if (location.objectid != objectid)
  1031. goto out;
  1032. } else {
  1033. goto out;
  1034. }
  1035. btrfs_release_path(path);
  1036. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0);
  1037. if (IS_ERR(di)) {
  1038. ret = PTR_ERR(di);
  1039. goto out;
  1040. } else if (di) {
  1041. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  1042. if (location.objectid == objectid)
  1043. ret = 1;
  1044. }
  1045. out:
  1046. btrfs_release_path(path);
  1047. return ret;
  1048. }
  1049. /*
  1050. * helper function to check a log tree for a named back reference in
  1051. * an inode. This is used to decide if a back reference that is
  1052. * found in the subvolume conflicts with what we find in the log.
  1053. *
  1054. * inode backreferences may have multiple refs in a single item,
  1055. * during replay we process one reference at a time, and we don't
  1056. * want to delete valid links to a file from the subvolume if that
  1057. * link is also in the log.
  1058. */
  1059. static noinline int backref_in_log(struct btrfs_root *log,
  1060. struct btrfs_key *key,
  1061. u64 ref_objectid,
  1062. const struct fscrypt_str *name)
  1063. {
  1064. BTRFS_PATH_AUTO_FREE(path);
  1065. int ret;
  1066. path = btrfs_alloc_path();
  1067. if (!path)
  1068. return -ENOMEM;
  1069. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  1070. if (ret < 0)
  1071. return ret;
  1072. if (ret == 1)
  1073. return 0;
  1074. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1075. ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
  1076. path->slots[0],
  1077. ref_objectid, name);
  1078. else
  1079. ret = !!btrfs_find_name_in_backref(path->nodes[0],
  1080. path->slots[0], name);
  1081. return ret;
  1082. }
  1083. static int unlink_refs_not_in_log(struct walk_control *wc,
  1084. struct btrfs_key *search_key,
  1085. struct btrfs_inode *dir,
  1086. struct btrfs_inode *inode)
  1087. {
  1088. struct extent_buffer *leaf = wc->subvol_path->nodes[0];
  1089. unsigned long ptr;
  1090. unsigned long ptr_end;
  1091. /*
  1092. * Check all the names in this back reference to see if they are in the
  1093. * log. If so, we allow them to stay otherwise they must be unlinked as
  1094. * a conflict.
  1095. */
  1096. ptr = btrfs_item_ptr_offset(leaf, wc->subvol_path->slots[0]);
  1097. ptr_end = ptr + btrfs_item_size(leaf, wc->subvol_path->slots[0]);
  1098. while (ptr < ptr_end) {
  1099. struct fscrypt_str victim_name;
  1100. struct btrfs_inode_ref *victim_ref;
  1101. int ret;
  1102. victim_ref = (struct btrfs_inode_ref *)ptr;
  1103. ret = read_alloc_one_name(leaf, (victim_ref + 1),
  1104. btrfs_inode_ref_name_len(leaf, victim_ref),
  1105. &victim_name);
  1106. if (ret) {
  1107. btrfs_abort_log_replay(wc, ret,
  1108. "failed to allocate name for inode %llu parent dir %llu root %llu",
  1109. btrfs_ino(inode), btrfs_ino(dir),
  1110. btrfs_root_id(inode->root));
  1111. return ret;
  1112. }
  1113. ret = backref_in_log(wc->log, search_key, btrfs_ino(dir), &victim_name);
  1114. if (ret) {
  1115. if (ret < 0) {
  1116. btrfs_abort_log_replay(wc, ret,
  1117. "failed to check if backref is in log tree for inode %llu parent dir %llu name %.*s root %llu",
  1118. btrfs_ino(inode), btrfs_ino(dir),
  1119. victim_name.len, victim_name.name,
  1120. btrfs_root_id(inode->root));
  1121. kfree(victim_name.name);
  1122. return ret;
  1123. }
  1124. kfree(victim_name.name);
  1125. ptr = (unsigned long)(victim_ref + 1) + victim_name.len;
  1126. continue;
  1127. }
  1128. inc_nlink(&inode->vfs_inode);
  1129. btrfs_release_path(wc->subvol_path);
  1130. ret = unlink_inode_for_log_replay(wc, dir, inode, &victim_name);
  1131. kfree(victim_name.name);
  1132. if (ret)
  1133. return ret;
  1134. return -EAGAIN;
  1135. }
  1136. return 0;
  1137. }
  1138. static int unlink_extrefs_not_in_log(struct walk_control *wc,
  1139. struct btrfs_key *search_key,
  1140. struct btrfs_inode *dir,
  1141. struct btrfs_inode *inode)
  1142. {
  1143. struct extent_buffer *leaf = wc->subvol_path->nodes[0];
  1144. const unsigned long base = btrfs_item_ptr_offset(leaf, wc->subvol_path->slots[0]);
  1145. const u32 item_size = btrfs_item_size(leaf, wc->subvol_path->slots[0]);
  1146. u32 cur_offset = 0;
  1147. while (cur_offset < item_size) {
  1148. struct btrfs_root *log_root = wc->log;
  1149. struct btrfs_inode_extref *extref;
  1150. struct fscrypt_str victim_name;
  1151. int ret;
  1152. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  1153. victim_name.len = btrfs_inode_extref_name_len(leaf, extref);
  1154. if (btrfs_inode_extref_parent(leaf, extref) != btrfs_ino(dir))
  1155. goto next;
  1156. ret = read_alloc_one_name(leaf, &extref->name, victim_name.len,
  1157. &victim_name);
  1158. if (ret) {
  1159. btrfs_abort_log_replay(wc, ret,
  1160. "failed to allocate name for inode %llu parent dir %llu root %llu",
  1161. btrfs_ino(inode), btrfs_ino(dir),
  1162. btrfs_root_id(inode->root));
  1163. return ret;
  1164. }
  1165. search_key->objectid = btrfs_ino(inode);
  1166. search_key->type = BTRFS_INODE_EXTREF_KEY;
  1167. search_key->offset = btrfs_extref_hash(btrfs_ino(dir),
  1168. victim_name.name,
  1169. victim_name.len);
  1170. ret = backref_in_log(log_root, search_key, btrfs_ino(dir), &victim_name);
  1171. if (ret) {
  1172. if (ret < 0) {
  1173. btrfs_abort_log_replay(wc, ret,
  1174. "failed to check if backref is in log tree for inode %llu parent dir %llu name %.*s root %llu",
  1175. btrfs_ino(inode), btrfs_ino(dir),
  1176. victim_name.len, victim_name.name,
  1177. btrfs_root_id(inode->root));
  1178. kfree(victim_name.name);
  1179. return ret;
  1180. }
  1181. kfree(victim_name.name);
  1182. next:
  1183. cur_offset += victim_name.len + sizeof(*extref);
  1184. continue;
  1185. }
  1186. inc_nlink(&inode->vfs_inode);
  1187. btrfs_release_path(wc->subvol_path);
  1188. ret = unlink_inode_for_log_replay(wc, dir, inode, &victim_name);
  1189. kfree(victim_name.name);
  1190. if (ret)
  1191. return ret;
  1192. return -EAGAIN;
  1193. }
  1194. return 0;
  1195. }
  1196. static inline int __add_inode_ref(struct walk_control *wc,
  1197. struct btrfs_inode *dir,
  1198. struct btrfs_inode *inode,
  1199. u64 ref_index, struct fscrypt_str *name)
  1200. {
  1201. int ret;
  1202. struct btrfs_trans_handle *trans = wc->trans;
  1203. struct btrfs_root *root = wc->root;
  1204. struct btrfs_dir_item *di;
  1205. struct btrfs_key search_key;
  1206. struct btrfs_inode_extref *extref;
  1207. again:
  1208. /* Search old style refs */
  1209. search_key.objectid = btrfs_ino(inode);
  1210. search_key.type = BTRFS_INODE_REF_KEY;
  1211. search_key.offset = btrfs_ino(dir);
  1212. ret = btrfs_search_slot(NULL, root, &search_key, wc->subvol_path, 0, 0);
  1213. if (ret < 0) {
  1214. btrfs_abort_log_replay(wc, ret,
  1215. "failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu",
  1216. BTRFS_KEY_FMT_VALUE(&search_key),
  1217. btrfs_root_id(root));
  1218. return ret;
  1219. } else if (ret == 0) {
  1220. /*
  1221. * Are we trying to overwrite a back ref for the root directory?
  1222. * If so, we're done.
  1223. */
  1224. if (search_key.objectid == search_key.offset)
  1225. return 1;
  1226. ret = unlink_refs_not_in_log(wc, &search_key, dir, inode);
  1227. if (ret == -EAGAIN)
  1228. goto again;
  1229. else if (ret)
  1230. return ret;
  1231. }
  1232. btrfs_release_path(wc->subvol_path);
  1233. /* Same search but for extended refs */
  1234. extref = btrfs_lookup_inode_extref(root, wc->subvol_path, name,
  1235. btrfs_ino(inode), btrfs_ino(dir));
  1236. if (IS_ERR(extref)) {
  1237. return PTR_ERR(extref);
  1238. } else if (extref) {
  1239. ret = unlink_extrefs_not_in_log(wc, &search_key, dir, inode);
  1240. if (ret == -EAGAIN)
  1241. goto again;
  1242. else if (ret)
  1243. return ret;
  1244. }
  1245. btrfs_release_path(wc->subvol_path);
  1246. /* look for a conflicting sequence number */
  1247. di = btrfs_lookup_dir_index_item(trans, root, wc->subvol_path, btrfs_ino(dir),
  1248. ref_index, name, 0);
  1249. if (IS_ERR(di)) {
  1250. ret = PTR_ERR(di);
  1251. btrfs_abort_log_replay(wc, ret,
  1252. "failed to lookup dir index item for dir %llu ref_index %llu name %.*s root %llu",
  1253. btrfs_ino(dir), ref_index, name->len,
  1254. name->name, btrfs_root_id(root));
  1255. return ret;
  1256. } else if (di) {
  1257. ret = drop_one_dir_item(wc, dir, di);
  1258. if (ret)
  1259. return ret;
  1260. }
  1261. btrfs_release_path(wc->subvol_path);
  1262. /* look for a conflicting name */
  1263. di = btrfs_lookup_dir_item(trans, root, wc->subvol_path, btrfs_ino(dir), name, 0);
  1264. if (IS_ERR(di)) {
  1265. ret = PTR_ERR(di);
  1266. btrfs_abort_log_replay(wc, ret,
  1267. "failed to lookup dir item for dir %llu name %.*s root %llu",
  1268. btrfs_ino(dir), name->len, name->name,
  1269. btrfs_root_id(root));
  1270. return ret;
  1271. } else if (di) {
  1272. ret = drop_one_dir_item(wc, dir, di);
  1273. if (ret)
  1274. return ret;
  1275. }
  1276. btrfs_release_path(wc->subvol_path);
  1277. return 0;
  1278. }
  1279. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1280. struct fscrypt_str *name, u64 *index,
  1281. u64 *parent_objectid)
  1282. {
  1283. struct btrfs_inode_extref *extref;
  1284. int ret;
  1285. extref = (struct btrfs_inode_extref *)ref_ptr;
  1286. ret = read_alloc_one_name(eb, &extref->name,
  1287. btrfs_inode_extref_name_len(eb, extref), name);
  1288. if (ret)
  1289. return ret;
  1290. if (index)
  1291. *index = btrfs_inode_extref_index(eb, extref);
  1292. if (parent_objectid)
  1293. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  1294. return 0;
  1295. }
  1296. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1297. struct fscrypt_str *name, u64 *index)
  1298. {
  1299. struct btrfs_inode_ref *ref;
  1300. int ret;
  1301. ref = (struct btrfs_inode_ref *)ref_ptr;
  1302. ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref),
  1303. name);
  1304. if (ret)
  1305. return ret;
  1306. if (index)
  1307. *index = btrfs_inode_ref_index(eb, ref);
  1308. return 0;
  1309. }
  1310. /*
  1311. * Take an inode reference item from the log tree and iterate all names from the
  1312. * inode reference item in the subvolume tree with the same key (if it exists).
  1313. * For any name that is not in the inode reference item from the log tree, do a
  1314. * proper unlink of that name (that is, remove its entry from the inode
  1315. * reference item and both dir index keys).
  1316. */
  1317. static int unlink_old_inode_refs(struct walk_control *wc, struct btrfs_inode *inode)
  1318. {
  1319. struct btrfs_root *root = wc->root;
  1320. int ret;
  1321. unsigned long ref_ptr;
  1322. unsigned long ref_end;
  1323. struct extent_buffer *eb;
  1324. again:
  1325. btrfs_release_path(wc->subvol_path);
  1326. ret = btrfs_search_slot(NULL, root, &wc->log_key, wc->subvol_path, 0, 0);
  1327. if (ret > 0) {
  1328. ret = 0;
  1329. goto out;
  1330. }
  1331. if (ret < 0) {
  1332. btrfs_abort_log_replay(wc, ret,
  1333. "failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu",
  1334. BTRFS_KEY_FMT_VALUE(&wc->log_key),
  1335. btrfs_root_id(root));
  1336. goto out;
  1337. }
  1338. eb = wc->subvol_path->nodes[0];
  1339. ref_ptr = btrfs_item_ptr_offset(eb, wc->subvol_path->slots[0]);
  1340. ref_end = ref_ptr + btrfs_item_size(eb, wc->subvol_path->slots[0]);
  1341. while (ref_ptr < ref_end) {
  1342. struct fscrypt_str name;
  1343. u64 parent_id;
  1344. if (wc->log_key.type == BTRFS_INODE_EXTREF_KEY) {
  1345. ret = extref_get_fields(eb, ref_ptr, &name,
  1346. NULL, &parent_id);
  1347. if (ret) {
  1348. btrfs_abort_log_replay(wc, ret,
  1349. "failed to get extref details for inode %llu root %llu",
  1350. btrfs_ino(inode),
  1351. btrfs_root_id(root));
  1352. goto out;
  1353. }
  1354. } else {
  1355. parent_id = wc->log_key.offset;
  1356. ret = ref_get_fields(eb, ref_ptr, &name, NULL);
  1357. if (ret) {
  1358. btrfs_abort_log_replay(wc, ret,
  1359. "failed to get ref details for inode %llu parent_id %llu root %llu",
  1360. btrfs_ino(inode), parent_id,
  1361. btrfs_root_id(root));
  1362. goto out;
  1363. }
  1364. }
  1365. if (wc->log_key.type == BTRFS_INODE_EXTREF_KEY)
  1366. ret = !!btrfs_find_name_in_ext_backref(wc->log_leaf, wc->log_slot,
  1367. parent_id, &name);
  1368. else
  1369. ret = !!btrfs_find_name_in_backref(wc->log_leaf, wc->log_slot,
  1370. &name);
  1371. if (!ret) {
  1372. struct btrfs_inode *dir;
  1373. btrfs_release_path(wc->subvol_path);
  1374. dir = btrfs_iget_logging(parent_id, root);
  1375. if (IS_ERR(dir)) {
  1376. ret = PTR_ERR(dir);
  1377. kfree(name.name);
  1378. btrfs_abort_log_replay(wc, ret,
  1379. "failed to lookup dir inode %llu root %llu",
  1380. parent_id, btrfs_root_id(root));
  1381. goto out;
  1382. }
  1383. ret = unlink_inode_for_log_replay(wc, dir, inode, &name);
  1384. kfree(name.name);
  1385. iput(&dir->vfs_inode);
  1386. if (ret)
  1387. goto out;
  1388. goto again;
  1389. }
  1390. kfree(name.name);
  1391. ref_ptr += name.len;
  1392. if (wc->log_key.type == BTRFS_INODE_EXTREF_KEY)
  1393. ref_ptr += sizeof(struct btrfs_inode_extref);
  1394. else
  1395. ref_ptr += sizeof(struct btrfs_inode_ref);
  1396. }
  1397. ret = 0;
  1398. out:
  1399. btrfs_release_path(wc->subvol_path);
  1400. return ret;
  1401. }
  1402. /*
  1403. * Replay one inode back reference item found in the log tree.
  1404. * Path is for temporary use by this function (it should be released on return).
  1405. */
  1406. static noinline int add_inode_ref(struct walk_control *wc)
  1407. {
  1408. struct btrfs_trans_handle *trans = wc->trans;
  1409. struct btrfs_root *root = wc->root;
  1410. struct btrfs_inode *dir = NULL;
  1411. struct btrfs_inode *inode = NULL;
  1412. unsigned long ref_ptr;
  1413. unsigned long ref_end;
  1414. struct fscrypt_str name = { 0 };
  1415. int ret;
  1416. const bool is_extref_item = (wc->log_key.type == BTRFS_INODE_EXTREF_KEY);
  1417. u64 parent_objectid;
  1418. u64 inode_objectid;
  1419. u64 ref_index = 0;
  1420. int ref_struct_size;
  1421. ref_ptr = btrfs_item_ptr_offset(wc->log_leaf, wc->log_slot);
  1422. ref_end = ref_ptr + btrfs_item_size(wc->log_leaf, wc->log_slot);
  1423. if (is_extref_item) {
  1424. struct btrfs_inode_extref *r;
  1425. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1426. r = (struct btrfs_inode_extref *)ref_ptr;
  1427. parent_objectid = btrfs_inode_extref_parent(wc->log_leaf, r);
  1428. } else {
  1429. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1430. parent_objectid = wc->log_key.offset;
  1431. }
  1432. inode_objectid = wc->log_key.objectid;
  1433. /*
  1434. * it is possible that we didn't log all the parent directories
  1435. * for a given inode. If we don't find the dir, just don't
  1436. * copy the back ref in. The link count fixup code will take
  1437. * care of the rest
  1438. */
  1439. dir = btrfs_iget_logging(parent_objectid, root);
  1440. if (IS_ERR(dir)) {
  1441. ret = PTR_ERR(dir);
  1442. if (ret == -ENOENT)
  1443. ret = 0;
  1444. else
  1445. btrfs_abort_log_replay(wc, ret,
  1446. "failed to lookup dir inode %llu root %llu",
  1447. parent_objectid, btrfs_root_id(root));
  1448. dir = NULL;
  1449. goto out;
  1450. }
  1451. inode = btrfs_iget_logging(inode_objectid, root);
  1452. if (IS_ERR(inode)) {
  1453. ret = PTR_ERR(inode);
  1454. btrfs_abort_log_replay(wc, ret,
  1455. "failed to lookup inode %llu root %llu",
  1456. inode_objectid, btrfs_root_id(root));
  1457. inode = NULL;
  1458. goto out;
  1459. }
  1460. while (ref_ptr < ref_end) {
  1461. if (is_extref_item) {
  1462. ret = extref_get_fields(wc->log_leaf, ref_ptr, &name,
  1463. &ref_index, &parent_objectid);
  1464. if (ret) {
  1465. btrfs_abort_log_replay(wc, ret,
  1466. "failed to get extref details for inode %llu root %llu",
  1467. btrfs_ino(inode),
  1468. btrfs_root_id(root));
  1469. goto out;
  1470. }
  1471. /*
  1472. * parent object can change from one array
  1473. * item to another.
  1474. */
  1475. if (!dir) {
  1476. dir = btrfs_iget_logging(parent_objectid, root);
  1477. if (IS_ERR(dir)) {
  1478. ret = PTR_ERR(dir);
  1479. dir = NULL;
  1480. /*
  1481. * A new parent dir may have not been
  1482. * logged and not exist in the subvolume
  1483. * tree, see the comment above before
  1484. * the loop when getting the first
  1485. * parent dir.
  1486. */
  1487. if (ret == -ENOENT) {
  1488. /*
  1489. * The next extref may refer to
  1490. * another parent dir that
  1491. * exists, so continue.
  1492. */
  1493. ret = 0;
  1494. goto next;
  1495. } else {
  1496. btrfs_abort_log_replay(wc, ret,
  1497. "failed to lookup dir inode %llu root %llu",
  1498. parent_objectid,
  1499. btrfs_root_id(root));
  1500. }
  1501. goto out;
  1502. }
  1503. }
  1504. } else {
  1505. ret = ref_get_fields(wc->log_leaf, ref_ptr, &name, &ref_index);
  1506. if (ret) {
  1507. btrfs_abort_log_replay(wc, ret,
  1508. "failed to get ref details for inode %llu parent_objectid %llu root %llu",
  1509. btrfs_ino(inode),
  1510. parent_objectid,
  1511. btrfs_root_id(root));
  1512. goto out;
  1513. }
  1514. }
  1515. ret = inode_in_dir(root, wc->subvol_path, btrfs_ino(dir),
  1516. btrfs_ino(inode), ref_index, &name);
  1517. if (ret < 0) {
  1518. btrfs_abort_log_replay(wc, ret,
  1519. "failed to check if inode %llu is in dir %llu ref_index %llu name %.*s root %llu",
  1520. btrfs_ino(inode), btrfs_ino(dir),
  1521. ref_index, name.len, name.name,
  1522. btrfs_root_id(root));
  1523. goto out;
  1524. } else if (ret == 0) {
  1525. /*
  1526. * look for a conflicting back reference in the
  1527. * metadata. if we find one we have to unlink that name
  1528. * of the file before we add our new link. Later on, we
  1529. * overwrite any existing back reference, and we don't
  1530. * want to create dangling pointers in the directory.
  1531. */
  1532. ret = __add_inode_ref(wc, dir, inode, ref_index, &name);
  1533. if (ret) {
  1534. if (ret == 1)
  1535. ret = 0;
  1536. goto out;
  1537. }
  1538. /* insert our name */
  1539. ret = btrfs_add_link(trans, dir, inode, &name, 0, ref_index);
  1540. if (ret) {
  1541. btrfs_abort_log_replay(wc, ret,
  1542. "failed to add link for inode %llu in dir %llu ref_index %llu name %.*s root %llu",
  1543. btrfs_ino(inode),
  1544. btrfs_ino(dir), ref_index,
  1545. name.len, name.name,
  1546. btrfs_root_id(root));
  1547. goto out;
  1548. }
  1549. ret = btrfs_update_inode(trans, inode);
  1550. if (ret) {
  1551. btrfs_abort_log_replay(wc, ret,
  1552. "failed to update inode %llu root %llu",
  1553. btrfs_ino(inode),
  1554. btrfs_root_id(root));
  1555. goto out;
  1556. }
  1557. }
  1558. /* Else, ret == 1, we already have a perfect match, we're done. */
  1559. next:
  1560. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len;
  1561. kfree(name.name);
  1562. name.name = NULL;
  1563. if (is_extref_item && dir) {
  1564. iput(&dir->vfs_inode);
  1565. dir = NULL;
  1566. }
  1567. }
  1568. /*
  1569. * Before we overwrite the inode reference item in the subvolume tree
  1570. * with the item from the log tree, we must unlink all names from the
  1571. * parent directory that are in the subvolume's tree inode reference
  1572. * item, otherwise we end up with an inconsistent subvolume tree where
  1573. * dir index entries exist for a name but there is no inode reference
  1574. * item with the same name.
  1575. */
  1576. ret = unlink_old_inode_refs(wc, inode);
  1577. if (ret)
  1578. goto out;
  1579. /* finally write the back reference in the inode */
  1580. ret = overwrite_item(wc);
  1581. out:
  1582. btrfs_release_path(wc->subvol_path);
  1583. kfree(name.name);
  1584. if (dir)
  1585. iput(&dir->vfs_inode);
  1586. if (inode)
  1587. iput(&inode->vfs_inode);
  1588. return ret;
  1589. }
  1590. static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path)
  1591. {
  1592. int ret = 0;
  1593. int name_len;
  1594. unsigned int nlink = 0;
  1595. u32 item_size;
  1596. u32 cur_offset = 0;
  1597. u64 inode_objectid = btrfs_ino(inode);
  1598. u64 offset = 0;
  1599. unsigned long ptr;
  1600. struct btrfs_inode_extref *extref;
  1601. struct extent_buffer *leaf;
  1602. while (1) {
  1603. ret = btrfs_find_one_extref(inode->root, inode_objectid, offset,
  1604. path, &extref, &offset);
  1605. if (ret)
  1606. break;
  1607. leaf = path->nodes[0];
  1608. item_size = btrfs_item_size(leaf, path->slots[0]);
  1609. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1610. cur_offset = 0;
  1611. while (cur_offset < item_size) {
  1612. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1613. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1614. nlink++;
  1615. cur_offset += name_len + sizeof(*extref);
  1616. }
  1617. offset++;
  1618. btrfs_release_path(path);
  1619. }
  1620. btrfs_release_path(path);
  1621. if (ret < 0 && ret != -ENOENT)
  1622. return ret;
  1623. return nlink;
  1624. }
  1625. static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path)
  1626. {
  1627. int ret;
  1628. struct btrfs_key key;
  1629. unsigned int nlink = 0;
  1630. unsigned long ptr;
  1631. unsigned long ptr_end;
  1632. int name_len;
  1633. u64 ino = btrfs_ino(inode);
  1634. key.objectid = ino;
  1635. key.type = BTRFS_INODE_REF_KEY;
  1636. key.offset = (u64)-1;
  1637. while (1) {
  1638. ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
  1639. if (ret < 0)
  1640. break;
  1641. if (ret > 0) {
  1642. if (path->slots[0] == 0)
  1643. break;
  1644. path->slots[0]--;
  1645. }
  1646. process_slot:
  1647. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1648. path->slots[0]);
  1649. if (key.objectid != ino ||
  1650. key.type != BTRFS_INODE_REF_KEY)
  1651. break;
  1652. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1653. ptr_end = ptr + btrfs_item_size(path->nodes[0],
  1654. path->slots[0]);
  1655. while (ptr < ptr_end) {
  1656. struct btrfs_inode_ref *ref;
  1657. ref = (struct btrfs_inode_ref *)ptr;
  1658. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1659. ref);
  1660. ptr = (unsigned long)(ref + 1) + name_len;
  1661. nlink++;
  1662. }
  1663. if (key.offset == 0)
  1664. break;
  1665. if (path->slots[0] > 0) {
  1666. path->slots[0]--;
  1667. goto process_slot;
  1668. }
  1669. key.offset--;
  1670. btrfs_release_path(path);
  1671. }
  1672. btrfs_release_path(path);
  1673. return nlink;
  1674. }
  1675. /*
  1676. * There are a few corners where the link count of the file can't
  1677. * be properly maintained during replay. So, instead of adding
  1678. * lots of complexity to the log code, we just scan the backrefs
  1679. * for any file that has been through replay.
  1680. *
  1681. * The scan will update the link count on the inode to reflect the
  1682. * number of back refs found. If it goes down to zero, the iput
  1683. * will free the inode.
  1684. */
  1685. static noinline int fixup_inode_link_count(struct walk_control *wc,
  1686. struct btrfs_inode *inode)
  1687. {
  1688. struct btrfs_trans_handle *trans = wc->trans;
  1689. struct btrfs_root *root = inode->root;
  1690. int ret;
  1691. u64 nlink = 0;
  1692. const u64 ino = btrfs_ino(inode);
  1693. ret = count_inode_refs(inode, wc->subvol_path);
  1694. if (ret < 0)
  1695. goto out;
  1696. nlink = ret;
  1697. ret = count_inode_extrefs(inode, wc->subvol_path);
  1698. if (ret < 0)
  1699. goto out;
  1700. nlink += ret;
  1701. ret = 0;
  1702. if (nlink != inode->vfs_inode.i_nlink) {
  1703. set_nlink(&inode->vfs_inode, nlink);
  1704. ret = btrfs_update_inode(trans, inode);
  1705. if (ret)
  1706. goto out;
  1707. }
  1708. if (S_ISDIR(inode->vfs_inode.i_mode))
  1709. inode->index_cnt = (u64)-1;
  1710. if (inode->vfs_inode.i_nlink == 0) {
  1711. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  1712. ret = replay_dir_deletes(wc, ino, true);
  1713. if (ret)
  1714. goto out;
  1715. }
  1716. ret = btrfs_insert_orphan_item(trans, root, ino);
  1717. if (ret == -EEXIST)
  1718. ret = 0;
  1719. }
  1720. out:
  1721. btrfs_release_path(wc->subvol_path);
  1722. return ret;
  1723. }
  1724. static noinline int fixup_inode_link_counts(struct walk_control *wc)
  1725. {
  1726. int ret;
  1727. struct btrfs_key key;
  1728. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1729. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1730. key.offset = (u64)-1;
  1731. while (1) {
  1732. struct btrfs_trans_handle *trans = wc->trans;
  1733. struct btrfs_root *root = wc->root;
  1734. struct btrfs_inode *inode;
  1735. ret = btrfs_search_slot(trans, root, &key, wc->subvol_path, -1, 1);
  1736. if (ret < 0)
  1737. break;
  1738. if (ret == 1) {
  1739. ret = 0;
  1740. if (wc->subvol_path->slots[0] == 0)
  1741. break;
  1742. wc->subvol_path->slots[0]--;
  1743. }
  1744. btrfs_item_key_to_cpu(wc->subvol_path->nodes[0], &key, wc->subvol_path->slots[0]);
  1745. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1746. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1747. break;
  1748. ret = btrfs_del_item(trans, root, wc->subvol_path);
  1749. if (ret)
  1750. break;
  1751. btrfs_release_path(wc->subvol_path);
  1752. inode = btrfs_iget_logging(key.offset, root);
  1753. if (IS_ERR(inode)) {
  1754. ret = PTR_ERR(inode);
  1755. break;
  1756. }
  1757. ret = fixup_inode_link_count(wc, inode);
  1758. iput(&inode->vfs_inode);
  1759. if (ret)
  1760. break;
  1761. /*
  1762. * fixup on a directory may create new entries,
  1763. * make sure we always look for the highest possible
  1764. * offset
  1765. */
  1766. key.offset = (u64)-1;
  1767. }
  1768. btrfs_release_path(wc->subvol_path);
  1769. return ret;
  1770. }
  1771. /*
  1772. * record a given inode in the fixup dir so we can check its link
  1773. * count when replay is done. The link count is incremented here
  1774. * so the inode won't go away until we check it
  1775. */
  1776. static noinline int link_to_fixup_dir(struct walk_control *wc, u64 objectid)
  1777. {
  1778. struct btrfs_trans_handle *trans = wc->trans;
  1779. struct btrfs_root *root = wc->root;
  1780. struct btrfs_key key;
  1781. int ret = 0;
  1782. struct btrfs_inode *inode;
  1783. struct inode *vfs_inode;
  1784. inode = btrfs_iget_logging(objectid, root);
  1785. if (IS_ERR(inode)) {
  1786. ret = PTR_ERR(inode);
  1787. btrfs_abort_log_replay(wc, ret,
  1788. "failed to lookup inode %llu root %llu",
  1789. objectid, btrfs_root_id(root));
  1790. return ret;
  1791. }
  1792. vfs_inode = &inode->vfs_inode;
  1793. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1794. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1795. key.offset = objectid;
  1796. ret = btrfs_insert_empty_item(trans, root, wc->subvol_path, &key, 0);
  1797. btrfs_release_path(wc->subvol_path);
  1798. if (ret == 0) {
  1799. if (!vfs_inode->i_nlink)
  1800. set_nlink(vfs_inode, 1);
  1801. else
  1802. inc_nlink(vfs_inode);
  1803. ret = btrfs_update_inode(trans, inode);
  1804. if (ret)
  1805. btrfs_abort_log_replay(wc, ret,
  1806. "failed to update inode %llu root %llu",
  1807. objectid, btrfs_root_id(root));
  1808. } else if (ret == -EEXIST) {
  1809. ret = 0;
  1810. } else {
  1811. btrfs_abort_log_replay(wc, ret,
  1812. "failed to insert fixup item for inode %llu root %llu",
  1813. objectid, btrfs_root_id(root));
  1814. }
  1815. iput(vfs_inode);
  1816. return ret;
  1817. }
  1818. /*
  1819. * when replaying the log for a directory, we only insert names
  1820. * for inodes that actually exist. This means an fsync on a directory
  1821. * does not implicitly fsync all the new files in it
  1822. */
  1823. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1824. struct btrfs_root *root,
  1825. u64 dirid, u64 index,
  1826. const struct fscrypt_str *name,
  1827. struct btrfs_key *location)
  1828. {
  1829. struct btrfs_inode *inode;
  1830. struct btrfs_inode *dir;
  1831. int ret;
  1832. inode = btrfs_iget_logging(location->objectid, root);
  1833. if (IS_ERR(inode))
  1834. return PTR_ERR(inode);
  1835. dir = btrfs_iget_logging(dirid, root);
  1836. if (IS_ERR(dir)) {
  1837. iput(&inode->vfs_inode);
  1838. return PTR_ERR(dir);
  1839. }
  1840. ret = btrfs_add_link(trans, dir, inode, name, 1, index);
  1841. /* FIXME, put inode into FIXUP list */
  1842. iput(&inode->vfs_inode);
  1843. iput(&dir->vfs_inode);
  1844. return ret;
  1845. }
  1846. static int delete_conflicting_dir_entry(struct walk_control *wc,
  1847. struct btrfs_inode *dir,
  1848. struct btrfs_dir_item *dst_di,
  1849. const struct btrfs_key *log_key,
  1850. u8 log_flags,
  1851. bool exists)
  1852. {
  1853. struct btrfs_key found_key;
  1854. btrfs_dir_item_key_to_cpu(wc->subvol_path->nodes[0], dst_di, &found_key);
  1855. /* The existing dentry points to the same inode, don't delete it. */
  1856. if (found_key.objectid == log_key->objectid &&
  1857. found_key.type == log_key->type &&
  1858. found_key.offset == log_key->offset &&
  1859. btrfs_dir_flags(wc->subvol_path->nodes[0], dst_di) == log_flags)
  1860. return 1;
  1861. /*
  1862. * Don't drop the conflicting directory entry if the inode for the new
  1863. * entry doesn't exist.
  1864. */
  1865. if (!exists)
  1866. return 0;
  1867. return drop_one_dir_item(wc, dir, dst_di);
  1868. }
  1869. /*
  1870. * take a single entry in a log directory item and replay it into
  1871. * the subvolume.
  1872. *
  1873. * if a conflicting item exists in the subdirectory already,
  1874. * the inode it points to is unlinked and put into the link count
  1875. * fix up tree.
  1876. *
  1877. * If a name from the log points to a file or directory that does
  1878. * not exist in the FS, it is skipped. fsyncs on directories
  1879. * do not force down inodes inside that directory, just changes to the
  1880. * names or unlinks in a directory.
  1881. *
  1882. * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
  1883. * non-existing inode) and 1 if the name was replayed.
  1884. */
  1885. static noinline int replay_one_name(struct walk_control *wc, struct btrfs_dir_item *di)
  1886. {
  1887. struct btrfs_trans_handle *trans = wc->trans;
  1888. struct btrfs_root *root = wc->root;
  1889. struct fscrypt_str name = { 0 };
  1890. struct btrfs_dir_item *dir_dst_di;
  1891. struct btrfs_dir_item *index_dst_di;
  1892. bool dir_dst_matches = false;
  1893. bool index_dst_matches = false;
  1894. struct btrfs_key log_key;
  1895. struct btrfs_key search_key;
  1896. struct btrfs_inode *dir;
  1897. u8 log_flags;
  1898. bool exists;
  1899. int ret;
  1900. bool update_size = true;
  1901. bool name_added = false;
  1902. dir = btrfs_iget_logging(wc->log_key.objectid, root);
  1903. if (IS_ERR(dir)) {
  1904. ret = PTR_ERR(dir);
  1905. btrfs_abort_log_replay(wc, ret,
  1906. "failed to lookup dir inode %llu root %llu",
  1907. wc->log_key.objectid, btrfs_root_id(root));
  1908. return ret;
  1909. }
  1910. ret = read_alloc_one_name(wc->log_leaf, di + 1,
  1911. btrfs_dir_name_len(wc->log_leaf, di), &name);
  1912. if (ret) {
  1913. btrfs_abort_log_replay(wc, ret,
  1914. "failed to allocate name for dir %llu root %llu",
  1915. btrfs_ino(dir), btrfs_root_id(root));
  1916. goto out;
  1917. }
  1918. log_flags = btrfs_dir_flags(wc->log_leaf, di);
  1919. btrfs_dir_item_key_to_cpu(wc->log_leaf, di, &log_key);
  1920. ret = btrfs_lookup_inode(trans, root, wc->subvol_path, &log_key, 0);
  1921. btrfs_release_path(wc->subvol_path);
  1922. if (ret < 0) {
  1923. btrfs_abort_log_replay(wc, ret,
  1924. "failed to lookup inode %llu root %llu",
  1925. log_key.objectid, btrfs_root_id(root));
  1926. goto out;
  1927. }
  1928. exists = (ret == 0);
  1929. ret = 0;
  1930. dir_dst_di = btrfs_lookup_dir_item(trans, root, wc->subvol_path,
  1931. wc->log_key.objectid, &name, 1);
  1932. if (IS_ERR(dir_dst_di)) {
  1933. ret = PTR_ERR(dir_dst_di);
  1934. btrfs_abort_log_replay(wc, ret,
  1935. "failed to lookup dir item for dir %llu name %.*s root %llu",
  1936. wc->log_key.objectid, name.len, name.name,
  1937. btrfs_root_id(root));
  1938. goto out;
  1939. } else if (dir_dst_di) {
  1940. ret = delete_conflicting_dir_entry(wc, dir, dir_dst_di,
  1941. &log_key, log_flags, exists);
  1942. if (ret < 0) {
  1943. btrfs_abort_log_replay(wc, ret,
  1944. "failed to delete conflicting entry for dir %llu name %.*s root %llu",
  1945. btrfs_ino(dir), name.len, name.name,
  1946. btrfs_root_id(root));
  1947. goto out;
  1948. }
  1949. dir_dst_matches = (ret == 1);
  1950. }
  1951. btrfs_release_path(wc->subvol_path);
  1952. index_dst_di = btrfs_lookup_dir_index_item(trans, root, wc->subvol_path,
  1953. wc->log_key.objectid,
  1954. wc->log_key.offset, &name, 1);
  1955. if (IS_ERR(index_dst_di)) {
  1956. ret = PTR_ERR(index_dst_di);
  1957. btrfs_abort_log_replay(wc, ret,
  1958. "failed to lookup dir index item for dir %llu name %.*s root %llu",
  1959. wc->log_key.objectid, name.len, name.name,
  1960. btrfs_root_id(root));
  1961. goto out;
  1962. } else if (index_dst_di) {
  1963. ret = delete_conflicting_dir_entry(wc, dir, index_dst_di,
  1964. &log_key, log_flags, exists);
  1965. if (ret < 0) {
  1966. btrfs_abort_log_replay(wc, ret,
  1967. "failed to delete conflicting entry for dir %llu name %.*s root %llu",
  1968. btrfs_ino(dir), name.len, name.name,
  1969. btrfs_root_id(root));
  1970. goto out;
  1971. }
  1972. index_dst_matches = (ret == 1);
  1973. }
  1974. btrfs_release_path(wc->subvol_path);
  1975. if (dir_dst_matches && index_dst_matches) {
  1976. ret = 0;
  1977. update_size = false;
  1978. goto out;
  1979. }
  1980. /*
  1981. * Check if the inode reference exists in the log for the given name,
  1982. * inode and parent inode
  1983. */
  1984. search_key.objectid = log_key.objectid;
  1985. search_key.type = BTRFS_INODE_REF_KEY;
  1986. search_key.offset = wc->log_key.objectid;
  1987. ret = backref_in_log(root->log_root, &search_key, 0, &name);
  1988. if (ret < 0) {
  1989. btrfs_abort_log_replay(wc, ret,
  1990. "failed to check if ref item is logged for inode %llu dir %llu name %.*s root %llu",
  1991. search_key.objectid, btrfs_ino(dir),
  1992. name.len, name.name, btrfs_root_id(root));
  1993. goto out;
  1994. } else if (ret) {
  1995. /* The dentry will be added later. */
  1996. ret = 0;
  1997. update_size = false;
  1998. goto out;
  1999. }
  2000. search_key.objectid = log_key.objectid;
  2001. search_key.type = BTRFS_INODE_EXTREF_KEY;
  2002. search_key.offset = btrfs_extref_hash(wc->log_key.objectid, name.name, name.len);
  2003. ret = backref_in_log(root->log_root, &search_key, wc->log_key.objectid, &name);
  2004. if (ret < 0) {
  2005. btrfs_abort_log_replay(wc, ret,
  2006. "failed to check if extref item is logged for inode %llu dir %llu name %.*s root %llu",
  2007. search_key.objectid, btrfs_ino(dir),
  2008. name.len, name.name, btrfs_root_id(root));
  2009. goto out;
  2010. } else if (ret) {
  2011. /* The dentry will be added later. */
  2012. ret = 0;
  2013. update_size = false;
  2014. goto out;
  2015. }
  2016. ret = insert_one_name(trans, root, wc->log_key.objectid, wc->log_key.offset,
  2017. &name, &log_key);
  2018. if (ret && ret != -ENOENT && ret != -EEXIST) {
  2019. btrfs_abort_log_replay(wc, ret,
  2020. "failed to insert name %.*s for inode %llu dir %llu root %llu",
  2021. name.len, name.name, log_key.objectid,
  2022. btrfs_ino(dir), btrfs_root_id(root));
  2023. goto out;
  2024. }
  2025. if (!ret)
  2026. name_added = true;
  2027. update_size = false;
  2028. ret = 0;
  2029. out:
  2030. if (!ret && update_size) {
  2031. btrfs_i_size_write(dir, dir->vfs_inode.i_size + name.len * 2);
  2032. ret = btrfs_update_inode(trans, dir);
  2033. if (ret)
  2034. btrfs_abort_log_replay(wc, ret,
  2035. "failed to update dir inode %llu root %llu",
  2036. btrfs_ino(dir), btrfs_root_id(root));
  2037. }
  2038. kfree(name.name);
  2039. iput(&dir->vfs_inode);
  2040. if (!ret && name_added)
  2041. ret = 1;
  2042. return ret;
  2043. }
  2044. /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */
  2045. static noinline int replay_one_dir_item(struct walk_control *wc)
  2046. {
  2047. int ret;
  2048. struct btrfs_dir_item *di;
  2049. /* We only log dir index keys, which only contain a single dir item. */
  2050. ASSERT(wc->log_key.type == BTRFS_DIR_INDEX_KEY,
  2051. "wc->log_key.type=%u", wc->log_key.type);
  2052. di = btrfs_item_ptr(wc->log_leaf, wc->log_slot, struct btrfs_dir_item);
  2053. ret = replay_one_name(wc, di);
  2054. if (ret < 0)
  2055. return ret;
  2056. /*
  2057. * If this entry refers to a non-directory (directories can not have a
  2058. * link count > 1) and it was added in the transaction that was not
  2059. * committed, make sure we fixup the link count of the inode the entry
  2060. * points to. Otherwise something like the following would result in a
  2061. * directory pointing to an inode with a wrong link that does not account
  2062. * for this dir entry:
  2063. *
  2064. * mkdir testdir
  2065. * touch testdir/foo
  2066. * touch testdir/bar
  2067. * sync
  2068. *
  2069. * ln testdir/bar testdir/bar_link
  2070. * ln testdir/foo testdir/foo_link
  2071. * xfs_io -c "fsync" testdir/bar
  2072. *
  2073. * <power failure>
  2074. *
  2075. * mount fs, log replay happens
  2076. *
  2077. * File foo would remain with a link count of 1 when it has two entries
  2078. * pointing to it in the directory testdir. This would make it impossible
  2079. * to ever delete the parent directory has it would result in stale
  2080. * dentries that can never be deleted.
  2081. */
  2082. if (ret == 1 && btrfs_dir_ftype(wc->log_leaf, di) != BTRFS_FT_DIR) {
  2083. struct btrfs_key di_key;
  2084. btrfs_dir_item_key_to_cpu(wc->log_leaf, di, &di_key);
  2085. ret = link_to_fixup_dir(wc, di_key.objectid);
  2086. }
  2087. return ret;
  2088. }
  2089. /*
  2090. * directory replay has two parts. There are the standard directory
  2091. * items in the log copied from the subvolume, and range items
  2092. * created in the log while the subvolume was logged.
  2093. *
  2094. * The range items tell us which parts of the key space the log
  2095. * is authoritative for. During replay, if a key in the subvolume
  2096. * directory is in a logged range item, but not actually in the log
  2097. * that means it was deleted from the directory before the fsync
  2098. * and should be removed.
  2099. */
  2100. static noinline int find_dir_range(struct btrfs_root *root,
  2101. struct btrfs_path *path,
  2102. u64 dirid,
  2103. u64 *start_ret, u64 *end_ret)
  2104. {
  2105. struct btrfs_key key;
  2106. u64 found_end;
  2107. struct btrfs_dir_log_item *item;
  2108. int ret;
  2109. int nritems;
  2110. if (*start_ret == (u64)-1)
  2111. return 1;
  2112. key.objectid = dirid;
  2113. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2114. key.offset = *start_ret;
  2115. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2116. if (ret < 0)
  2117. goto out;
  2118. if (ret > 0) {
  2119. if (path->slots[0] == 0)
  2120. goto out;
  2121. path->slots[0]--;
  2122. }
  2123. if (ret != 0)
  2124. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  2125. if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
  2126. ret = 1;
  2127. goto next;
  2128. }
  2129. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2130. struct btrfs_dir_log_item);
  2131. found_end = btrfs_dir_log_end(path->nodes[0], item);
  2132. if (*start_ret >= key.offset && *start_ret <= found_end) {
  2133. ret = 0;
  2134. *start_ret = key.offset;
  2135. *end_ret = found_end;
  2136. goto out;
  2137. }
  2138. ret = 1;
  2139. next:
  2140. /* check the next slot in the tree to see if it is a valid item */
  2141. nritems = btrfs_header_nritems(path->nodes[0]);
  2142. path->slots[0]++;
  2143. if (path->slots[0] >= nritems) {
  2144. ret = btrfs_next_leaf(root, path);
  2145. if (ret)
  2146. goto out;
  2147. }
  2148. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  2149. if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
  2150. ret = 1;
  2151. goto out;
  2152. }
  2153. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2154. struct btrfs_dir_log_item);
  2155. found_end = btrfs_dir_log_end(path->nodes[0], item);
  2156. *start_ret = key.offset;
  2157. *end_ret = found_end;
  2158. ret = 0;
  2159. out:
  2160. btrfs_release_path(path);
  2161. return ret;
  2162. }
  2163. /*
  2164. * this looks for a given directory item in the log. If the directory
  2165. * item is not in the log, the item is removed and the inode it points
  2166. * to is unlinked
  2167. */
  2168. static noinline int check_item_in_log(struct walk_control *wc,
  2169. struct btrfs_path *log_path,
  2170. struct btrfs_inode *dir,
  2171. struct btrfs_key *dir_key,
  2172. bool force_remove)
  2173. {
  2174. struct btrfs_trans_handle *trans = wc->trans;
  2175. struct btrfs_root *root = dir->root;
  2176. int ret;
  2177. struct extent_buffer *eb;
  2178. int slot;
  2179. struct btrfs_dir_item *di;
  2180. struct fscrypt_str name = { 0 };
  2181. struct btrfs_inode *inode = NULL;
  2182. struct btrfs_key location;
  2183. /*
  2184. * Currently we only log dir index keys. Even if we replay a log created
  2185. * by an older kernel that logged both dir index and dir item keys, all
  2186. * we need to do is process the dir index keys, we (and our caller) can
  2187. * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY).
  2188. */
  2189. ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY, "dir_key->type=%u", dir_key->type);
  2190. eb = wc->subvol_path->nodes[0];
  2191. slot = wc->subvol_path->slots[0];
  2192. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2193. ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
  2194. if (ret) {
  2195. btrfs_abort_log_replay(wc, ret,
  2196. "failed to allocate name for dir %llu index %llu root %llu",
  2197. btrfs_ino(dir), dir_key->offset,
  2198. btrfs_root_id(root));
  2199. goto out;
  2200. }
  2201. if (!force_remove) {
  2202. struct btrfs_dir_item *log_di;
  2203. log_di = btrfs_lookup_dir_index_item(trans, wc->log, log_path,
  2204. dir_key->objectid,
  2205. dir_key->offset, &name, 0);
  2206. if (IS_ERR(log_di)) {
  2207. ret = PTR_ERR(log_di);
  2208. btrfs_abort_log_replay(wc, ret,
  2209. "failed to lookup dir index item for dir %llu index %llu name %.*s root %llu",
  2210. btrfs_ino(dir), dir_key->offset,
  2211. name.len, name.name,
  2212. btrfs_root_id(root));
  2213. goto out;
  2214. } else if (log_di) {
  2215. /* The dentry exists in the log, we have nothing to do. */
  2216. ret = 0;
  2217. goto out;
  2218. }
  2219. }
  2220. btrfs_dir_item_key_to_cpu(eb, di, &location);
  2221. btrfs_release_path(wc->subvol_path);
  2222. btrfs_release_path(log_path);
  2223. inode = btrfs_iget_logging(location.objectid, root);
  2224. if (IS_ERR(inode)) {
  2225. ret = PTR_ERR(inode);
  2226. inode = NULL;
  2227. btrfs_abort_log_replay(wc, ret,
  2228. "failed to lookup inode %llu root %llu",
  2229. location.objectid, btrfs_root_id(root));
  2230. goto out;
  2231. }
  2232. ret = link_to_fixup_dir(wc, location.objectid);
  2233. if (ret)
  2234. goto out;
  2235. inc_nlink(&inode->vfs_inode);
  2236. ret = unlink_inode_for_log_replay(wc, dir, inode, &name);
  2237. /*
  2238. * Unlike dir item keys, dir index keys can only have one name (entry) in
  2239. * them, as there are no key collisions since each key has a unique offset
  2240. * (an index number), so we're done.
  2241. */
  2242. out:
  2243. btrfs_release_path(wc->subvol_path);
  2244. btrfs_release_path(log_path);
  2245. kfree(name.name);
  2246. if (inode)
  2247. iput(&inode->vfs_inode);
  2248. return ret;
  2249. }
  2250. static int replay_xattr_deletes(struct walk_control *wc)
  2251. {
  2252. struct btrfs_trans_handle *trans = wc->trans;
  2253. struct btrfs_root *root = wc->root;
  2254. struct btrfs_root *log = wc->log;
  2255. struct btrfs_key search_key;
  2256. BTRFS_PATH_AUTO_FREE(log_path);
  2257. const u64 ino = wc->log_key.objectid;
  2258. int nritems;
  2259. int ret;
  2260. log_path = btrfs_alloc_path();
  2261. if (!log_path) {
  2262. btrfs_abort_log_replay(wc, -ENOMEM, "failed to allocate path");
  2263. return -ENOMEM;
  2264. }
  2265. search_key.objectid = ino;
  2266. search_key.type = BTRFS_XATTR_ITEM_KEY;
  2267. search_key.offset = 0;
  2268. again:
  2269. ret = btrfs_search_slot(NULL, root, &search_key, wc->subvol_path, 0, 0);
  2270. if (ret < 0) {
  2271. btrfs_abort_log_replay(wc, ret,
  2272. "failed to search xattrs for inode %llu root %llu",
  2273. ino, btrfs_root_id(root));
  2274. goto out;
  2275. }
  2276. process_leaf:
  2277. nritems = btrfs_header_nritems(wc->subvol_path->nodes[0]);
  2278. for (int i = wc->subvol_path->slots[0]; i < nritems; i++) {
  2279. struct btrfs_key key;
  2280. struct btrfs_dir_item *di;
  2281. struct btrfs_dir_item *log_di;
  2282. u32 total_size;
  2283. u32 cur;
  2284. btrfs_item_key_to_cpu(wc->subvol_path->nodes[0], &key, i);
  2285. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
  2286. ret = 0;
  2287. goto out;
  2288. }
  2289. di = btrfs_item_ptr(wc->subvol_path->nodes[0], i, struct btrfs_dir_item);
  2290. total_size = btrfs_item_size(wc->subvol_path->nodes[0], i);
  2291. cur = 0;
  2292. while (cur < total_size) {
  2293. u16 name_len = btrfs_dir_name_len(wc->subvol_path->nodes[0], di);
  2294. u16 data_len = btrfs_dir_data_len(wc->subvol_path->nodes[0], di);
  2295. u32 this_len = sizeof(*di) + name_len + data_len;
  2296. char *name;
  2297. name = kmalloc(name_len, GFP_NOFS);
  2298. if (!name) {
  2299. ret = -ENOMEM;
  2300. btrfs_abort_log_replay(wc, ret,
  2301. "failed to allocate memory for name of length %u",
  2302. name_len);
  2303. goto out;
  2304. }
  2305. read_extent_buffer(wc->subvol_path->nodes[0], name,
  2306. (unsigned long)(di + 1), name_len);
  2307. log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
  2308. name, name_len, 0);
  2309. btrfs_release_path(log_path);
  2310. if (!log_di) {
  2311. /* Doesn't exist in log tree, so delete it. */
  2312. btrfs_release_path(wc->subvol_path);
  2313. di = btrfs_lookup_xattr(trans, root, wc->subvol_path, ino,
  2314. name, name_len, -1);
  2315. if (IS_ERR(di)) {
  2316. ret = PTR_ERR(di);
  2317. btrfs_abort_log_replay(wc, ret,
  2318. "failed to lookup xattr with name %.*s for inode %llu root %llu",
  2319. name_len, name, ino,
  2320. btrfs_root_id(root));
  2321. kfree(name);
  2322. goto out;
  2323. }
  2324. ASSERT(di);
  2325. ret = btrfs_delete_one_dir_name(trans, root,
  2326. wc->subvol_path, di);
  2327. if (ret) {
  2328. btrfs_abort_log_replay(wc, ret,
  2329. "failed to delete xattr with name %.*s for inode %llu root %llu",
  2330. name_len, name, ino,
  2331. btrfs_root_id(root));
  2332. kfree(name);
  2333. goto out;
  2334. }
  2335. btrfs_release_path(wc->subvol_path);
  2336. kfree(name);
  2337. search_key = key;
  2338. goto again;
  2339. }
  2340. if (IS_ERR(log_di)) {
  2341. ret = PTR_ERR(log_di);
  2342. btrfs_abort_log_replay(wc, ret,
  2343. "failed to lookup xattr in log tree with name %.*s for inode %llu root %llu",
  2344. name_len, name, ino,
  2345. btrfs_root_id(root));
  2346. kfree(name);
  2347. goto out;
  2348. }
  2349. kfree(name);
  2350. cur += this_len;
  2351. di = (struct btrfs_dir_item *)((char *)di + this_len);
  2352. }
  2353. }
  2354. ret = btrfs_next_leaf(root, wc->subvol_path);
  2355. if (ret > 0)
  2356. ret = 0;
  2357. else if (ret == 0)
  2358. goto process_leaf;
  2359. else
  2360. btrfs_abort_log_replay(wc, ret,
  2361. "failed to get next leaf in subvolume root %llu",
  2362. btrfs_root_id(root));
  2363. out:
  2364. btrfs_release_path(wc->subvol_path);
  2365. return ret;
  2366. }
  2367. /*
  2368. * deletion replay happens before we copy any new directory items
  2369. * out of the log or out of backreferences from inodes. It
  2370. * scans the log to find ranges of keys that log is authoritative for,
  2371. * and then scans the directory to find items in those ranges that are
  2372. * not present in the log.
  2373. *
  2374. * Anything we don't find in the log is unlinked and removed from the
  2375. * directory.
  2376. */
  2377. static noinline int replay_dir_deletes(struct walk_control *wc,
  2378. u64 dirid, bool del_all)
  2379. {
  2380. struct btrfs_root *root = wc->root;
  2381. struct btrfs_root *log = (del_all ? NULL : wc->log);
  2382. u64 range_start;
  2383. u64 range_end;
  2384. int ret = 0;
  2385. struct btrfs_key dir_key;
  2386. struct btrfs_key found_key;
  2387. BTRFS_PATH_AUTO_FREE(log_path);
  2388. struct btrfs_inode *dir;
  2389. dir_key.objectid = dirid;
  2390. dir_key.type = BTRFS_DIR_INDEX_KEY;
  2391. log_path = btrfs_alloc_path();
  2392. if (!log_path) {
  2393. btrfs_abort_log_replay(wc, -ENOMEM, "failed to allocate path");
  2394. return -ENOMEM;
  2395. }
  2396. dir = btrfs_iget_logging(dirid, root);
  2397. /*
  2398. * It isn't an error if the inode isn't there, that can happen because
  2399. * we replay the deletes before we copy in the inode item from the log.
  2400. */
  2401. if (IS_ERR(dir)) {
  2402. ret = PTR_ERR(dir);
  2403. if (ret == -ENOENT)
  2404. ret = 0;
  2405. else
  2406. btrfs_abort_log_replay(wc, ret,
  2407. "failed to lookup dir inode %llu root %llu",
  2408. dirid, btrfs_root_id(root));
  2409. return ret;
  2410. }
  2411. range_start = 0;
  2412. range_end = 0;
  2413. while (1) {
  2414. if (del_all)
  2415. range_end = (u64)-1;
  2416. else {
  2417. ret = find_dir_range(log, wc->subvol_path, dirid,
  2418. &range_start, &range_end);
  2419. if (ret < 0) {
  2420. btrfs_abort_log_replay(wc, ret,
  2421. "failed to find range for dir %llu in log tree root %llu",
  2422. dirid, btrfs_root_id(root));
  2423. goto out;
  2424. } else if (ret > 0) {
  2425. break;
  2426. }
  2427. }
  2428. dir_key.offset = range_start;
  2429. while (1) {
  2430. int nritems;
  2431. ret = btrfs_search_slot(NULL, root, &dir_key,
  2432. wc->subvol_path, 0, 0);
  2433. if (ret < 0) {
  2434. btrfs_abort_log_replay(wc, ret,
  2435. "failed to search root %llu for key " BTRFS_KEY_FMT,
  2436. btrfs_root_id(root),
  2437. BTRFS_KEY_FMT_VALUE(&dir_key));
  2438. goto out;
  2439. }
  2440. nritems = btrfs_header_nritems(wc->subvol_path->nodes[0]);
  2441. if (wc->subvol_path->slots[0] >= nritems) {
  2442. ret = btrfs_next_leaf(root, wc->subvol_path);
  2443. if (ret == 1) {
  2444. break;
  2445. } else if (ret < 0) {
  2446. btrfs_abort_log_replay(wc, ret,
  2447. "failed to get next leaf in subvolume root %llu",
  2448. btrfs_root_id(root));
  2449. goto out;
  2450. }
  2451. }
  2452. btrfs_item_key_to_cpu(wc->subvol_path->nodes[0], &found_key,
  2453. wc->subvol_path->slots[0]);
  2454. if (found_key.objectid != dirid ||
  2455. found_key.type != dir_key.type) {
  2456. ret = 0;
  2457. goto out;
  2458. }
  2459. if (found_key.offset > range_end)
  2460. break;
  2461. ret = check_item_in_log(wc, log_path, dir, &found_key, del_all);
  2462. if (ret)
  2463. goto out;
  2464. if (found_key.offset == (u64)-1)
  2465. break;
  2466. dir_key.offset = found_key.offset + 1;
  2467. }
  2468. btrfs_release_path(wc->subvol_path);
  2469. if (range_end == (u64)-1)
  2470. break;
  2471. range_start = range_end + 1;
  2472. }
  2473. ret = 0;
  2474. out:
  2475. btrfs_release_path(wc->subvol_path);
  2476. iput(&dir->vfs_inode);
  2477. return ret;
  2478. }
  2479. /*
  2480. * the process_func used to replay items from the log tree. This
  2481. * gets called in two different stages. The first stage just looks
  2482. * for inodes and makes sure they are all copied into the subvolume.
  2483. *
  2484. * The second stage copies all the other item types from the log into
  2485. * the subvolume. The two stage approach is slower, but gets rid of
  2486. * lots of complexity around inodes referencing other inodes that exist
  2487. * only in the log (references come from either directory items or inode
  2488. * back refs).
  2489. */
  2490. static int replay_one_buffer(struct extent_buffer *eb,
  2491. struct walk_control *wc, u64 gen, int level)
  2492. {
  2493. int nritems;
  2494. struct btrfs_tree_parent_check check = {
  2495. .transid = gen,
  2496. .level = level
  2497. };
  2498. struct btrfs_root *root = wc->root;
  2499. struct btrfs_trans_handle *trans = wc->trans;
  2500. int ret;
  2501. if (level != 0)
  2502. return 0;
  2503. /*
  2504. * Set to NULL since it was not yet read and in case we abort log replay
  2505. * on error, we have no valid log tree leaf to dump.
  2506. */
  2507. wc->log_leaf = NULL;
  2508. ret = btrfs_read_extent_buffer(eb, &check);
  2509. if (ret) {
  2510. btrfs_abort_log_replay(wc, ret,
  2511. "failed to read log tree leaf %llu for root %llu",
  2512. eb->start, btrfs_root_id(root));
  2513. return ret;
  2514. }
  2515. ASSERT(wc->subvol_path == NULL);
  2516. wc->subvol_path = btrfs_alloc_path();
  2517. if (!wc->subvol_path) {
  2518. btrfs_abort_log_replay(wc, -ENOMEM, "failed to allocate path");
  2519. return -ENOMEM;
  2520. }
  2521. wc->log_leaf = eb;
  2522. nritems = btrfs_header_nritems(eb);
  2523. for (wc->log_slot = 0; wc->log_slot < nritems; wc->log_slot++) {
  2524. struct btrfs_inode_item *inode_item = NULL;
  2525. btrfs_item_key_to_cpu(eb, &wc->log_key, wc->log_slot);
  2526. if (wc->log_key.type == BTRFS_INODE_ITEM_KEY) {
  2527. inode_item = btrfs_item_ptr(eb, wc->log_slot,
  2528. struct btrfs_inode_item);
  2529. /*
  2530. * An inode with no links is either:
  2531. *
  2532. * 1) A tmpfile (O_TMPFILE) that got fsync'ed and never
  2533. * got linked before the fsync, skip it, as replaying
  2534. * it is pointless since it would be deleted later.
  2535. * We skip logging tmpfiles, but it's always possible
  2536. * we are replaying a log created with a kernel that
  2537. * used to log tmpfiles;
  2538. *
  2539. * 2) A non-tmpfile which got its last link deleted
  2540. * while holding an open fd on it and later got
  2541. * fsynced through that fd. We always log the
  2542. * parent inodes when inode->last_unlink_trans is
  2543. * set to the current transaction, so ignore all the
  2544. * inode items for this inode. We will delete the
  2545. * inode when processing the parent directory with
  2546. * replay_dir_deletes().
  2547. */
  2548. if (btrfs_inode_nlink(eb, inode_item) == 0) {
  2549. wc->ignore_cur_inode = true;
  2550. continue;
  2551. } else {
  2552. wc->ignore_cur_inode = false;
  2553. }
  2554. }
  2555. /* Inode keys are done during the first stage. */
  2556. if (wc->log_key.type == BTRFS_INODE_ITEM_KEY &&
  2557. wc->stage == LOG_WALK_REPLAY_INODES) {
  2558. u32 mode;
  2559. ret = replay_xattr_deletes(wc);
  2560. if (ret)
  2561. break;
  2562. mode = btrfs_inode_mode(eb, inode_item);
  2563. if (S_ISDIR(mode)) {
  2564. ret = replay_dir_deletes(wc, wc->log_key.objectid, false);
  2565. if (ret)
  2566. break;
  2567. }
  2568. ret = overwrite_item(wc);
  2569. if (ret)
  2570. break;
  2571. /*
  2572. * Before replaying extents, truncate the inode to its
  2573. * size. We need to do it now and not after log replay
  2574. * because before an fsync we can have prealloc extents
  2575. * added beyond the inode's i_size. If we did it after,
  2576. * through orphan cleanup for example, we would drop
  2577. * those prealloc extents just after replaying them.
  2578. */
  2579. if (S_ISREG(mode)) {
  2580. struct btrfs_drop_extents_args drop_args = { 0 };
  2581. struct btrfs_inode *inode;
  2582. u64 from;
  2583. inode = btrfs_iget_logging(wc->log_key.objectid, root);
  2584. if (IS_ERR(inode)) {
  2585. ret = PTR_ERR(inode);
  2586. btrfs_abort_log_replay(wc, ret,
  2587. "failed to lookup inode %llu root %llu",
  2588. wc->log_key.objectid,
  2589. btrfs_root_id(root));
  2590. break;
  2591. }
  2592. from = ALIGN(i_size_read(&inode->vfs_inode),
  2593. root->fs_info->sectorsize);
  2594. drop_args.start = from;
  2595. drop_args.end = (u64)-1;
  2596. drop_args.drop_cache = true;
  2597. drop_args.path = wc->subvol_path;
  2598. ret = btrfs_drop_extents(trans, root, inode, &drop_args);
  2599. if (ret) {
  2600. btrfs_abort_log_replay(wc, ret,
  2601. "failed to drop extents for inode %llu root %llu offset %llu",
  2602. btrfs_ino(inode),
  2603. btrfs_root_id(root),
  2604. from);
  2605. } else {
  2606. inode_sub_bytes(&inode->vfs_inode,
  2607. drop_args.bytes_found);
  2608. /* Update the inode's nbytes. */
  2609. ret = btrfs_update_inode(trans, inode);
  2610. if (ret)
  2611. btrfs_abort_log_replay(wc, ret,
  2612. "failed to update inode %llu root %llu",
  2613. btrfs_ino(inode),
  2614. btrfs_root_id(root));
  2615. }
  2616. iput(&inode->vfs_inode);
  2617. if (ret)
  2618. break;
  2619. }
  2620. ret = link_to_fixup_dir(wc, wc->log_key.objectid);
  2621. if (ret)
  2622. break;
  2623. }
  2624. if (wc->ignore_cur_inode)
  2625. continue;
  2626. if (wc->log_key.type == BTRFS_DIR_INDEX_KEY &&
  2627. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2628. ret = replay_one_dir_item(wc);
  2629. if (ret)
  2630. break;
  2631. }
  2632. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2633. continue;
  2634. /* these keys are simply copied */
  2635. if (wc->log_key.type == BTRFS_XATTR_ITEM_KEY) {
  2636. ret = overwrite_item(wc);
  2637. if (ret)
  2638. break;
  2639. } else if (wc->log_key.type == BTRFS_INODE_REF_KEY ||
  2640. wc->log_key.type == BTRFS_INODE_EXTREF_KEY) {
  2641. ret = add_inode_ref(wc);
  2642. if (ret)
  2643. break;
  2644. } else if (wc->log_key.type == BTRFS_EXTENT_DATA_KEY) {
  2645. ret = replay_one_extent(wc);
  2646. if (ret)
  2647. break;
  2648. }
  2649. /*
  2650. * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the
  2651. * BTRFS_DIR_INDEX_KEY items which we use to derive the
  2652. * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an
  2653. * older kernel with such keys, ignore them.
  2654. */
  2655. }
  2656. btrfs_free_path(wc->subvol_path);
  2657. wc->subvol_path = NULL;
  2658. return ret;
  2659. }
  2660. static int clean_log_buffer(struct btrfs_trans_handle *trans,
  2661. struct extent_buffer *eb)
  2662. {
  2663. struct btrfs_fs_info *fs_info = eb->fs_info;
  2664. struct btrfs_block_group *bg;
  2665. btrfs_tree_lock(eb);
  2666. btrfs_clear_buffer_dirty(trans, eb);
  2667. wait_on_extent_buffer_writeback(eb);
  2668. btrfs_tree_unlock(eb);
  2669. if (trans) {
  2670. int ret;
  2671. ret = btrfs_pin_reserved_extent(trans, eb);
  2672. if (ret)
  2673. btrfs_abort_transaction(trans, ret);
  2674. return ret;
  2675. }
  2676. bg = btrfs_lookup_block_group(fs_info, eb->start);
  2677. if (!bg) {
  2678. btrfs_err(fs_info, "unable to find block group for %llu", eb->start);
  2679. btrfs_handle_fs_error(fs_info, -ENOENT, NULL);
  2680. return -ENOENT;
  2681. }
  2682. spin_lock(&bg->space_info->lock);
  2683. spin_lock(&bg->lock);
  2684. bg->reserved -= fs_info->nodesize;
  2685. bg->space_info->bytes_reserved -= fs_info->nodesize;
  2686. spin_unlock(&bg->lock);
  2687. spin_unlock(&bg->space_info->lock);
  2688. btrfs_put_block_group(bg);
  2689. return 0;
  2690. }
  2691. static noinline int walk_down_log_tree(struct btrfs_path *path, int *level,
  2692. struct walk_control *wc)
  2693. {
  2694. struct btrfs_trans_handle *trans = wc->trans;
  2695. struct btrfs_fs_info *fs_info = wc->log->fs_info;
  2696. u64 bytenr;
  2697. u64 ptr_gen;
  2698. struct extent_buffer *next;
  2699. struct extent_buffer *cur;
  2700. int ret = 0;
  2701. while (*level > 0) {
  2702. struct btrfs_tree_parent_check check = { 0 };
  2703. cur = path->nodes[*level];
  2704. WARN_ON(btrfs_header_level(cur) != *level);
  2705. if (path->slots[*level] >=
  2706. btrfs_header_nritems(cur))
  2707. break;
  2708. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2709. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2710. check.transid = ptr_gen;
  2711. check.level = *level - 1;
  2712. check.has_first_key = true;
  2713. btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]);
  2714. next = btrfs_find_create_tree_block(fs_info, bytenr,
  2715. btrfs_header_owner(cur),
  2716. *level - 1);
  2717. if (IS_ERR(next)) {
  2718. ret = PTR_ERR(next);
  2719. if (trans)
  2720. btrfs_abort_transaction(trans, ret);
  2721. else
  2722. btrfs_handle_fs_error(fs_info, ret, NULL);
  2723. return ret;
  2724. }
  2725. if (*level == 1) {
  2726. ret = wc->process_func(next, wc, ptr_gen, *level - 1);
  2727. if (ret) {
  2728. free_extent_buffer(next);
  2729. return ret;
  2730. }
  2731. path->slots[*level]++;
  2732. if (wc->free) {
  2733. ret = btrfs_read_extent_buffer(next, &check);
  2734. if (ret) {
  2735. free_extent_buffer(next);
  2736. if (trans)
  2737. btrfs_abort_transaction(trans, ret);
  2738. else
  2739. btrfs_handle_fs_error(fs_info, ret, NULL);
  2740. return ret;
  2741. }
  2742. ret = clean_log_buffer(trans, next);
  2743. if (ret) {
  2744. free_extent_buffer(next);
  2745. return ret;
  2746. }
  2747. }
  2748. free_extent_buffer(next);
  2749. continue;
  2750. }
  2751. ret = btrfs_read_extent_buffer(next, &check);
  2752. if (ret) {
  2753. free_extent_buffer(next);
  2754. if (trans)
  2755. btrfs_abort_transaction(trans, ret);
  2756. else
  2757. btrfs_handle_fs_error(fs_info, ret, NULL);
  2758. return ret;
  2759. }
  2760. if (path->nodes[*level-1])
  2761. free_extent_buffer(path->nodes[*level-1]);
  2762. path->nodes[*level-1] = next;
  2763. *level = btrfs_header_level(next);
  2764. path->slots[*level] = 0;
  2765. cond_resched();
  2766. }
  2767. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2768. cond_resched();
  2769. return 0;
  2770. }
  2771. static noinline int walk_up_log_tree(struct btrfs_path *path, int *level,
  2772. struct walk_control *wc)
  2773. {
  2774. int i;
  2775. int slot;
  2776. int ret;
  2777. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2778. slot = path->slots[i];
  2779. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2780. path->slots[i]++;
  2781. *level = i;
  2782. WARN_ON(*level == 0);
  2783. return 0;
  2784. } else {
  2785. ret = wc->process_func(path->nodes[*level], wc,
  2786. btrfs_header_generation(path->nodes[*level]),
  2787. *level);
  2788. if (ret)
  2789. return ret;
  2790. if (wc->free) {
  2791. ret = clean_log_buffer(wc->trans, path->nodes[*level]);
  2792. if (ret)
  2793. return ret;
  2794. }
  2795. free_extent_buffer(path->nodes[*level]);
  2796. path->nodes[*level] = NULL;
  2797. *level = i + 1;
  2798. }
  2799. }
  2800. return 1;
  2801. }
  2802. /*
  2803. * drop the reference count on the tree rooted at 'snap'. This traverses
  2804. * the tree freeing any blocks that have a ref count of zero after being
  2805. * decremented.
  2806. */
  2807. static int walk_log_tree(struct walk_control *wc)
  2808. {
  2809. struct btrfs_root *log = wc->log;
  2810. int ret = 0;
  2811. int wret;
  2812. int level;
  2813. BTRFS_PATH_AUTO_FREE(path);
  2814. int orig_level;
  2815. path = btrfs_alloc_path();
  2816. if (!path)
  2817. return -ENOMEM;
  2818. level = btrfs_header_level(log->node);
  2819. orig_level = level;
  2820. path->nodes[level] = log->node;
  2821. refcount_inc(&log->node->refs);
  2822. path->slots[level] = 0;
  2823. while (1) {
  2824. wret = walk_down_log_tree(path, &level, wc);
  2825. if (wret > 0)
  2826. break;
  2827. if (wret < 0)
  2828. return wret;
  2829. wret = walk_up_log_tree(path, &level, wc);
  2830. if (wret > 0)
  2831. break;
  2832. if (wret < 0)
  2833. return wret;
  2834. }
  2835. /* was the root node processed? if not, catch it here */
  2836. if (path->nodes[orig_level]) {
  2837. ret = wc->process_func(path->nodes[orig_level], wc,
  2838. btrfs_header_generation(path->nodes[orig_level]),
  2839. orig_level);
  2840. if (ret)
  2841. return ret;
  2842. if (wc->free)
  2843. ret = clean_log_buffer(wc->trans, path->nodes[orig_level]);
  2844. }
  2845. return ret;
  2846. }
  2847. /*
  2848. * helper function to update the item for a given subvolumes log root
  2849. * in the tree of log roots
  2850. */
  2851. static int update_log_root(struct btrfs_trans_handle *trans,
  2852. struct btrfs_root *log,
  2853. struct btrfs_root_item *root_item)
  2854. {
  2855. struct btrfs_fs_info *fs_info = log->fs_info;
  2856. int ret;
  2857. if (log->log_transid == 1) {
  2858. /* insert root item on the first sync */
  2859. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2860. &log->root_key, root_item);
  2861. } else {
  2862. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2863. &log->root_key, root_item);
  2864. }
  2865. return ret;
  2866. }
  2867. static void wait_log_commit(struct btrfs_root *root, int transid)
  2868. {
  2869. DEFINE_WAIT(wait);
  2870. int index = transid % 2;
  2871. /*
  2872. * we only allow two pending log transactions at a time,
  2873. * so we know that if ours is more than 2 older than the
  2874. * current transaction, we're done
  2875. */
  2876. for (;;) {
  2877. prepare_to_wait(&root->log_commit_wait[index],
  2878. &wait, TASK_UNINTERRUPTIBLE);
  2879. if (!(root->log_transid_committed < transid &&
  2880. atomic_read(&root->log_commit[index])))
  2881. break;
  2882. mutex_unlock(&root->log_mutex);
  2883. schedule();
  2884. mutex_lock(&root->log_mutex);
  2885. }
  2886. finish_wait(&root->log_commit_wait[index], &wait);
  2887. }
  2888. static void wait_for_writer(struct btrfs_root *root)
  2889. {
  2890. DEFINE_WAIT(wait);
  2891. for (;;) {
  2892. prepare_to_wait(&root->log_writer_wait, &wait,
  2893. TASK_UNINTERRUPTIBLE);
  2894. if (!atomic_read(&root->log_writers))
  2895. break;
  2896. mutex_unlock(&root->log_mutex);
  2897. schedule();
  2898. mutex_lock(&root->log_mutex);
  2899. }
  2900. finish_wait(&root->log_writer_wait, &wait);
  2901. }
  2902. void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct btrfs_inode *inode)
  2903. {
  2904. ctx->log_ret = 0;
  2905. ctx->log_transid = 0;
  2906. ctx->log_new_dentries = false;
  2907. ctx->logging_new_name = false;
  2908. ctx->logging_new_delayed_dentries = false;
  2909. ctx->logged_before = false;
  2910. ctx->inode = inode;
  2911. INIT_LIST_HEAD(&ctx->list);
  2912. INIT_LIST_HEAD(&ctx->ordered_extents);
  2913. INIT_LIST_HEAD(&ctx->conflict_inodes);
  2914. ctx->num_conflict_inodes = 0;
  2915. ctx->logging_conflict_inodes = false;
  2916. ctx->scratch_eb = NULL;
  2917. }
  2918. void btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx *ctx)
  2919. {
  2920. struct btrfs_inode *inode = ctx->inode;
  2921. if (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
  2922. !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
  2923. return;
  2924. /*
  2925. * Don't care about allocation failure. This is just for optimization,
  2926. * if we fail to allocate here, we will try again later if needed.
  2927. */
  2928. ctx->scratch_eb = alloc_dummy_extent_buffer(inode->root->fs_info, 0);
  2929. }
  2930. void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx)
  2931. {
  2932. struct btrfs_ordered_extent *ordered;
  2933. struct btrfs_ordered_extent *tmp;
  2934. btrfs_assert_inode_locked(ctx->inode);
  2935. list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
  2936. list_del_init(&ordered->log_list);
  2937. btrfs_put_ordered_extent(ordered);
  2938. }
  2939. }
  2940. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2941. struct btrfs_log_ctx *ctx)
  2942. {
  2943. mutex_lock(&root->log_mutex);
  2944. list_del_init(&ctx->list);
  2945. mutex_unlock(&root->log_mutex);
  2946. }
  2947. /*
  2948. * Invoked in log mutex context, or be sure there is no other task which
  2949. * can access the list.
  2950. */
  2951. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2952. int index, int error)
  2953. {
  2954. struct btrfs_log_ctx *ctx;
  2955. struct btrfs_log_ctx *safe;
  2956. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2957. list_del_init(&ctx->list);
  2958. ctx->log_ret = error;
  2959. }
  2960. }
  2961. /*
  2962. * Sends a given tree log down to the disk and updates the super blocks to
  2963. * record it. When this call is done, you know that any inodes previously
  2964. * logged are safely on disk only if it returns 0.
  2965. *
  2966. * Any other return value means you need to call btrfs_commit_transaction.
  2967. * Some of the edge cases for fsyncing directories that have had unlinks
  2968. * or renames done in the past mean that sometimes the only safe
  2969. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2970. * that has happened.
  2971. */
  2972. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2973. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2974. {
  2975. int index1;
  2976. int index2;
  2977. int mark;
  2978. int ret;
  2979. struct btrfs_fs_info *fs_info = root->fs_info;
  2980. struct btrfs_root *log = root->log_root;
  2981. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2982. struct btrfs_root_item new_root_item;
  2983. int log_transid = 0;
  2984. struct btrfs_log_ctx root_log_ctx;
  2985. struct blk_plug plug;
  2986. u64 log_root_start;
  2987. u64 log_root_level;
  2988. mutex_lock(&root->log_mutex);
  2989. log_transid = ctx->log_transid;
  2990. if (root->log_transid_committed >= log_transid) {
  2991. mutex_unlock(&root->log_mutex);
  2992. return ctx->log_ret;
  2993. }
  2994. index1 = log_transid % 2;
  2995. if (atomic_read(&root->log_commit[index1])) {
  2996. wait_log_commit(root, log_transid);
  2997. mutex_unlock(&root->log_mutex);
  2998. return ctx->log_ret;
  2999. }
  3000. ASSERT(log_transid == root->log_transid,
  3001. "log_transid=%d root->log_transid=%d", log_transid, root->log_transid);
  3002. atomic_set(&root->log_commit[index1], 1);
  3003. /* wait for previous tree log sync to complete */
  3004. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  3005. wait_log_commit(root, log_transid - 1);
  3006. while (1) {
  3007. int batch = atomic_read(&root->log_batch);
  3008. /* when we're on an ssd, just kick the log commit out */
  3009. if (!btrfs_test_opt(fs_info, SSD) &&
  3010. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  3011. mutex_unlock(&root->log_mutex);
  3012. schedule_timeout_uninterruptible(1);
  3013. mutex_lock(&root->log_mutex);
  3014. }
  3015. wait_for_writer(root);
  3016. if (batch == atomic_read(&root->log_batch))
  3017. break;
  3018. }
  3019. /* bail out if we need to do a full commit */
  3020. if (btrfs_need_log_full_commit(trans)) {
  3021. ret = BTRFS_LOG_FORCE_COMMIT;
  3022. mutex_unlock(&root->log_mutex);
  3023. goto out;
  3024. }
  3025. if (log_transid % 2 == 0)
  3026. mark = EXTENT_DIRTY_LOG1;
  3027. else
  3028. mark = EXTENT_DIRTY_LOG2;
  3029. /* we start IO on all the marked extents here, but we don't actually
  3030. * wait for them until later.
  3031. */
  3032. blk_start_plug(&plug);
  3033. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  3034. /*
  3035. * -EAGAIN happens when someone, e.g., a concurrent transaction
  3036. * commit, writes a dirty extent in this tree-log commit. This
  3037. * concurrent write will create a hole writing out the extents,
  3038. * and we cannot proceed on a zoned filesystem, requiring
  3039. * sequential writing. While we can bail out to a full commit
  3040. * here, but we can continue hoping the concurrent writing fills
  3041. * the hole.
  3042. */
  3043. if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
  3044. ret = 0;
  3045. if (ret) {
  3046. blk_finish_plug(&plug);
  3047. btrfs_set_log_full_commit(trans);
  3048. mutex_unlock(&root->log_mutex);
  3049. goto out;
  3050. }
  3051. /*
  3052. * We _must_ update under the root->log_mutex in order to make sure we
  3053. * have a consistent view of the log root we are trying to commit at
  3054. * this moment.
  3055. *
  3056. * We _must_ copy this into a local copy, because we are not holding the
  3057. * log_root_tree->log_mutex yet. This is important because when we
  3058. * commit the log_root_tree we must have a consistent view of the
  3059. * log_root_tree when we update the super block to point at the
  3060. * log_root_tree bytenr. If we update the log_root_tree here we'll race
  3061. * with the commit and possibly point at the new block which we may not
  3062. * have written out.
  3063. */
  3064. btrfs_set_root_node(&log->root_item, log->node);
  3065. memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
  3066. btrfs_set_root_log_transid(root, root->log_transid + 1);
  3067. log->log_transid = root->log_transid;
  3068. root->log_start_pid = 0;
  3069. /*
  3070. * IO has been started, blocks of the log tree have WRITTEN flag set
  3071. * in their headers. new modifications of the log will be written to
  3072. * new positions. so it's safe to allow log writers to go in.
  3073. */
  3074. mutex_unlock(&root->log_mutex);
  3075. if (btrfs_is_zoned(fs_info)) {
  3076. mutex_lock(&fs_info->tree_root->log_mutex);
  3077. if (!log_root_tree->node) {
  3078. ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
  3079. if (ret) {
  3080. mutex_unlock(&fs_info->tree_root->log_mutex);
  3081. blk_finish_plug(&plug);
  3082. goto out;
  3083. }
  3084. }
  3085. mutex_unlock(&fs_info->tree_root->log_mutex);
  3086. }
  3087. btrfs_init_log_ctx(&root_log_ctx, NULL);
  3088. mutex_lock(&log_root_tree->log_mutex);
  3089. index2 = log_root_tree->log_transid % 2;
  3090. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  3091. root_log_ctx.log_transid = log_root_tree->log_transid;
  3092. /*
  3093. * Now we are safe to update the log_root_tree because we're under the
  3094. * log_mutex, and we're a current writer so we're holding the commit
  3095. * open until we drop the log_mutex.
  3096. */
  3097. ret = update_log_root(trans, log, &new_root_item);
  3098. if (ret) {
  3099. list_del_init(&root_log_ctx.list);
  3100. blk_finish_plug(&plug);
  3101. btrfs_set_log_full_commit(trans);
  3102. if (ret != -ENOSPC)
  3103. btrfs_err(fs_info,
  3104. "failed to update log for root %llu ret %d",
  3105. btrfs_root_id(root), ret);
  3106. btrfs_wait_tree_log_extents(log, mark);
  3107. mutex_unlock(&log_root_tree->log_mutex);
  3108. goto out;
  3109. }
  3110. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  3111. blk_finish_plug(&plug);
  3112. list_del_init(&root_log_ctx.list);
  3113. mutex_unlock(&log_root_tree->log_mutex);
  3114. ret = root_log_ctx.log_ret;
  3115. goto out;
  3116. }
  3117. if (atomic_read(&log_root_tree->log_commit[index2])) {
  3118. blk_finish_plug(&plug);
  3119. ret = btrfs_wait_tree_log_extents(log, mark);
  3120. wait_log_commit(log_root_tree,
  3121. root_log_ctx.log_transid);
  3122. mutex_unlock(&log_root_tree->log_mutex);
  3123. if (!ret)
  3124. ret = root_log_ctx.log_ret;
  3125. goto out;
  3126. }
  3127. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid,
  3128. "root_log_ctx.log_transid=%d log_root_tree->log_transid=%d",
  3129. root_log_ctx.log_transid, log_root_tree->log_transid);
  3130. atomic_set(&log_root_tree->log_commit[index2], 1);
  3131. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  3132. wait_log_commit(log_root_tree,
  3133. root_log_ctx.log_transid - 1);
  3134. }
  3135. /*
  3136. * now that we've moved on to the tree of log tree roots,
  3137. * check the full commit flag again
  3138. */
  3139. if (btrfs_need_log_full_commit(trans)) {
  3140. blk_finish_plug(&plug);
  3141. btrfs_wait_tree_log_extents(log, mark);
  3142. mutex_unlock(&log_root_tree->log_mutex);
  3143. ret = BTRFS_LOG_FORCE_COMMIT;
  3144. goto out_wake_log_root;
  3145. }
  3146. ret = btrfs_write_marked_extents(fs_info,
  3147. &log_root_tree->dirty_log_pages,
  3148. EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
  3149. blk_finish_plug(&plug);
  3150. /*
  3151. * As described above, -EAGAIN indicates a hole in the extents. We
  3152. * cannot wait for these write outs since the waiting cause a
  3153. * deadlock. Bail out to the full commit instead.
  3154. */
  3155. if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
  3156. btrfs_set_log_full_commit(trans);
  3157. btrfs_wait_tree_log_extents(log, mark);
  3158. mutex_unlock(&log_root_tree->log_mutex);
  3159. goto out_wake_log_root;
  3160. } else if (ret) {
  3161. btrfs_set_log_full_commit(trans);
  3162. mutex_unlock(&log_root_tree->log_mutex);
  3163. goto out_wake_log_root;
  3164. }
  3165. ret = btrfs_wait_tree_log_extents(log, mark);
  3166. if (!ret)
  3167. ret = btrfs_wait_tree_log_extents(log_root_tree,
  3168. EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
  3169. if (ret) {
  3170. btrfs_set_log_full_commit(trans);
  3171. mutex_unlock(&log_root_tree->log_mutex);
  3172. goto out_wake_log_root;
  3173. }
  3174. log_root_start = log_root_tree->node->start;
  3175. log_root_level = btrfs_header_level(log_root_tree->node);
  3176. log_root_tree->log_transid++;
  3177. mutex_unlock(&log_root_tree->log_mutex);
  3178. /*
  3179. * Here we are guaranteed that nobody is going to write the superblock
  3180. * for the current transaction before us and that neither we do write
  3181. * our superblock before the previous transaction finishes its commit
  3182. * and writes its superblock, because:
  3183. *
  3184. * 1) We are holding a handle on the current transaction, so no body
  3185. * can commit it until we release the handle;
  3186. *
  3187. * 2) Before writing our superblock we acquire the tree_log_mutex, so
  3188. * if the previous transaction is still committing, and hasn't yet
  3189. * written its superblock, we wait for it to do it, because a
  3190. * transaction commit acquires the tree_log_mutex when the commit
  3191. * begins and releases it only after writing its superblock.
  3192. */
  3193. mutex_lock(&fs_info->tree_log_mutex);
  3194. /*
  3195. * The previous transaction writeout phase could have failed, and thus
  3196. * marked the fs in an error state. We must not commit here, as we
  3197. * could have updated our generation in the super_for_commit and
  3198. * writing the super here would result in transid mismatches. If there
  3199. * is an error here just bail.
  3200. */
  3201. if (BTRFS_FS_ERROR(fs_info)) {
  3202. ret = -EIO;
  3203. btrfs_set_log_full_commit(trans);
  3204. btrfs_abort_transaction(trans, ret);
  3205. mutex_unlock(&fs_info->tree_log_mutex);
  3206. goto out_wake_log_root;
  3207. }
  3208. btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
  3209. btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
  3210. ret = write_all_supers(fs_info, 1);
  3211. mutex_unlock(&fs_info->tree_log_mutex);
  3212. if (unlikely(ret)) {
  3213. btrfs_set_log_full_commit(trans);
  3214. btrfs_abort_transaction(trans, ret);
  3215. goto out_wake_log_root;
  3216. }
  3217. /*
  3218. * We know there can only be one task here, since we have not yet set
  3219. * root->log_commit[index1] to 0 and any task attempting to sync the
  3220. * log must wait for the previous log transaction to commit if it's
  3221. * still in progress or wait for the current log transaction commit if
  3222. * someone else already started it. We use <= and not < because the
  3223. * first log transaction has an ID of 0.
  3224. */
  3225. ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid,
  3226. "last_log_commit(root)=%d log_transid=%d",
  3227. btrfs_get_root_last_log_commit(root), log_transid);
  3228. btrfs_set_root_last_log_commit(root, log_transid);
  3229. out_wake_log_root:
  3230. mutex_lock(&log_root_tree->log_mutex);
  3231. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  3232. log_root_tree->log_transid_committed++;
  3233. atomic_set(&log_root_tree->log_commit[index2], 0);
  3234. mutex_unlock(&log_root_tree->log_mutex);
  3235. /*
  3236. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  3237. * all the updates above are seen by the woken threads. It might not be
  3238. * necessary, but proving that seems to be hard.
  3239. */
  3240. cond_wake_up(&log_root_tree->log_commit_wait[index2]);
  3241. out:
  3242. mutex_lock(&root->log_mutex);
  3243. btrfs_remove_all_log_ctxs(root, index1, ret);
  3244. root->log_transid_committed++;
  3245. atomic_set(&root->log_commit[index1], 0);
  3246. mutex_unlock(&root->log_mutex);
  3247. /*
  3248. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  3249. * all the updates above are seen by the woken threads. It might not be
  3250. * necessary, but proving that seems to be hard.
  3251. */
  3252. cond_wake_up(&root->log_commit_wait[index1]);
  3253. return ret;
  3254. }
  3255. static void free_log_tree(struct btrfs_trans_handle *trans,
  3256. struct btrfs_root *log)
  3257. {
  3258. int ret;
  3259. struct walk_control wc = {
  3260. .free = true,
  3261. .process_func = process_one_buffer,
  3262. .log = log,
  3263. .trans = trans,
  3264. };
  3265. if (log->node) {
  3266. ret = walk_log_tree(&wc);
  3267. if (ret) {
  3268. /*
  3269. * We weren't able to traverse the entire log tree, the
  3270. * typical scenario is getting an -EIO when reading an
  3271. * extent buffer of the tree, due to a previous writeback
  3272. * failure of it.
  3273. */
  3274. set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
  3275. &log->fs_info->fs_state);
  3276. /*
  3277. * Some extent buffers of the log tree may still be dirty
  3278. * and not yet written back to storage, because we may
  3279. * have updates to a log tree without syncing a log tree,
  3280. * such as during rename and link operations. So flush
  3281. * them out and wait for their writeback to complete, so
  3282. * that we properly cleanup their state and pages.
  3283. */
  3284. btrfs_write_marked_extents(log->fs_info,
  3285. &log->dirty_log_pages,
  3286. EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
  3287. btrfs_wait_tree_log_extents(log,
  3288. EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
  3289. if (trans)
  3290. btrfs_abort_transaction(trans, ret);
  3291. else
  3292. btrfs_handle_fs_error(log->fs_info, ret, NULL);
  3293. }
  3294. }
  3295. btrfs_extent_io_tree_release(&log->dirty_log_pages);
  3296. btrfs_extent_io_tree_release(&log->log_csum_range);
  3297. btrfs_put_root(log);
  3298. }
  3299. /*
  3300. * free all the extents used by the tree log. This should be called
  3301. * at commit time of the full transaction
  3302. */
  3303. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  3304. {
  3305. if (root->log_root) {
  3306. free_log_tree(trans, root->log_root);
  3307. root->log_root = NULL;
  3308. clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
  3309. }
  3310. return 0;
  3311. }
  3312. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  3313. struct btrfs_fs_info *fs_info)
  3314. {
  3315. if (fs_info->log_root_tree) {
  3316. free_log_tree(trans, fs_info->log_root_tree);
  3317. fs_info->log_root_tree = NULL;
  3318. clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
  3319. }
  3320. return 0;
  3321. }
  3322. static bool mark_inode_as_not_logged(const struct btrfs_trans_handle *trans,
  3323. struct btrfs_inode *inode)
  3324. {
  3325. bool ret = false;
  3326. /*
  3327. * Do this only if ->logged_trans is still 0 to prevent races with
  3328. * concurrent logging as we may see the inode not logged when
  3329. * inode_logged() is called but it gets logged after inode_logged() did
  3330. * not find it in the log tree and we end up setting ->logged_trans to a
  3331. * value less than trans->transid after the concurrent logging task has
  3332. * set it to trans->transid. As a consequence, subsequent rename, unlink
  3333. * and link operations may end up not logging new names and removing old
  3334. * names from the log.
  3335. */
  3336. spin_lock(&inode->lock);
  3337. if (inode->logged_trans == 0)
  3338. inode->logged_trans = trans->transid - 1;
  3339. else if (inode->logged_trans == trans->transid)
  3340. ret = true;
  3341. spin_unlock(&inode->lock);
  3342. return ret;
  3343. }
  3344. /*
  3345. * Check if an inode was logged in the current transaction. This correctly deals
  3346. * with the case where the inode was logged but has a logged_trans of 0, which
  3347. * happens if the inode is evicted and loaded again, as logged_trans is an in
  3348. * memory only field (not persisted).
  3349. *
  3350. * Returns 1 if the inode was logged before in the transaction, 0 if it was not,
  3351. * and < 0 on error.
  3352. */
  3353. static int inode_logged(const struct btrfs_trans_handle *trans,
  3354. struct btrfs_inode *inode,
  3355. struct btrfs_path *path_in)
  3356. {
  3357. struct btrfs_path *path = path_in;
  3358. struct btrfs_key key;
  3359. int ret;
  3360. /*
  3361. * Quick lockless call, since once ->logged_trans is set to the current
  3362. * transaction, we never set it to a lower value anywhere else.
  3363. */
  3364. if (data_race(inode->logged_trans) == trans->transid)
  3365. return 1;
  3366. /*
  3367. * If logged_trans is not 0 and not trans->transid, then we know the
  3368. * inode was not logged in this transaction, so we can return false
  3369. * right away. We take the lock to avoid a race caused by load/store
  3370. * tearing with a concurrent btrfs_log_inode() call or a concurrent task
  3371. * in this function further below - an update to trans->transid can be
  3372. * teared into two 32 bits updates for example, in which case we could
  3373. * see a positive value that is not trans->transid and assume the inode
  3374. * was not logged when it was.
  3375. */
  3376. spin_lock(&inode->lock);
  3377. if (inode->logged_trans == trans->transid) {
  3378. spin_unlock(&inode->lock);
  3379. return 1;
  3380. } else if (inode->logged_trans > 0) {
  3381. spin_unlock(&inode->lock);
  3382. return 0;
  3383. }
  3384. spin_unlock(&inode->lock);
  3385. /*
  3386. * If no log tree was created for this root in this transaction, then
  3387. * the inode can not have been logged in this transaction. In that case
  3388. * set logged_trans to anything greater than 0 and less than the current
  3389. * transaction's ID, to avoid the search below in a future call in case
  3390. * a log tree gets created after this.
  3391. */
  3392. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
  3393. return mark_inode_as_not_logged(trans, inode);
  3394. /*
  3395. * We have a log tree and the inode's logged_trans is 0. We can't tell
  3396. * for sure if the inode was logged before in this transaction by looking
  3397. * only at logged_trans. We could be pessimistic and assume it was, but
  3398. * that can lead to unnecessarily logging an inode during rename and link
  3399. * operations, and then further updating the log in followup rename and
  3400. * link operations, specially if it's a directory, which adds latency
  3401. * visible to applications doing a series of rename or link operations.
  3402. *
  3403. * A logged_trans of 0 here can mean several things:
  3404. *
  3405. * 1) The inode was never logged since the filesystem was mounted, and may
  3406. * or may have not been evicted and loaded again;
  3407. *
  3408. * 2) The inode was logged in a previous transaction, then evicted and
  3409. * then loaded again;
  3410. *
  3411. * 3) The inode was logged in the current transaction, then evicted and
  3412. * then loaded again.
  3413. *
  3414. * For cases 1) and 2) we don't want to return true, but we need to detect
  3415. * case 3) and return true. So we do a search in the log root for the inode
  3416. * item.
  3417. */
  3418. key.objectid = btrfs_ino(inode);
  3419. key.type = BTRFS_INODE_ITEM_KEY;
  3420. key.offset = 0;
  3421. if (!path) {
  3422. path = btrfs_alloc_path();
  3423. if (!path)
  3424. return -ENOMEM;
  3425. }
  3426. ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
  3427. if (path_in)
  3428. btrfs_release_path(path);
  3429. else
  3430. btrfs_free_path(path);
  3431. /*
  3432. * Logging an inode always results in logging its inode item. So if we
  3433. * did not find the item we know the inode was not logged for sure.
  3434. */
  3435. if (ret < 0) {
  3436. return ret;
  3437. } else if (ret > 0) {
  3438. /*
  3439. * Set logged_trans to a value greater than 0 and less then the
  3440. * current transaction to avoid doing the search in future calls.
  3441. */
  3442. return mark_inode_as_not_logged(trans, inode);
  3443. }
  3444. /*
  3445. * The inode was previously logged and then evicted, set logged_trans to
  3446. * the current transaction's ID, to avoid future tree searches as long as
  3447. * the inode is not evicted again.
  3448. */
  3449. spin_lock(&inode->lock);
  3450. inode->logged_trans = trans->transid;
  3451. spin_unlock(&inode->lock);
  3452. return 1;
  3453. }
  3454. /*
  3455. * Delete a directory entry from the log if it exists.
  3456. *
  3457. * Returns < 0 on error
  3458. * 1 if the entry does not exists
  3459. * 0 if the entry existed and was successfully deleted
  3460. */
  3461. static int del_logged_dentry(struct btrfs_trans_handle *trans,
  3462. struct btrfs_root *log,
  3463. struct btrfs_path *path,
  3464. u64 dir_ino,
  3465. const struct fscrypt_str *name,
  3466. u64 index)
  3467. {
  3468. struct btrfs_dir_item *di;
  3469. /*
  3470. * We only log dir index items of a directory, so we don't need to look
  3471. * for dir item keys.
  3472. */
  3473. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  3474. index, name, -1);
  3475. if (IS_ERR(di))
  3476. return PTR_ERR(di);
  3477. else if (!di)
  3478. return 1;
  3479. /*
  3480. * We do not need to update the size field of the directory's
  3481. * inode item because on log replay we update the field to reflect
  3482. * all existing entries in the directory (see overwrite_item()).
  3483. */
  3484. return btrfs_del_item(trans, log, path);
  3485. }
  3486. /*
  3487. * If both a file and directory are logged, and unlinks or renames are
  3488. * mixed in, we have a few interesting corners:
  3489. *
  3490. * create file X in dir Y
  3491. * link file X to X.link in dir Y
  3492. * fsync file X
  3493. * unlink file X but leave X.link
  3494. * fsync dir Y
  3495. *
  3496. * After a crash we would expect only X.link to exist. But file X
  3497. * didn't get fsync'd again so the log has back refs for X and X.link.
  3498. *
  3499. * We solve this by removing directory entries and inode backrefs from the
  3500. * log when a file that was logged in the current transaction is
  3501. * unlinked. Any later fsync will include the updated log entries, and
  3502. * we'll be able to reconstruct the proper directory items from backrefs.
  3503. *
  3504. * This optimizations allows us to avoid relogging the entire inode
  3505. * or the entire directory.
  3506. */
  3507. void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  3508. const struct fscrypt_str *name,
  3509. struct btrfs_inode *dir, u64 index)
  3510. {
  3511. struct btrfs_root *root = dir->root;
  3512. BTRFS_PATH_AUTO_FREE(path);
  3513. int ret;
  3514. ret = inode_logged(trans, dir, NULL);
  3515. if (ret == 0)
  3516. return;
  3517. if (ret < 0) {
  3518. btrfs_set_log_full_commit(trans);
  3519. return;
  3520. }
  3521. path = btrfs_alloc_path();
  3522. if (!path) {
  3523. btrfs_set_log_full_commit(trans);
  3524. return;
  3525. }
  3526. ret = join_running_log_trans(root);
  3527. ASSERT(ret == 0, "join_running_log_trans() ret=%d", ret);
  3528. if (WARN_ON(ret))
  3529. return;
  3530. mutex_lock(&dir->log_mutex);
  3531. ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir),
  3532. name, index);
  3533. mutex_unlock(&dir->log_mutex);
  3534. if (ret < 0)
  3535. btrfs_set_log_full_commit(trans);
  3536. btrfs_end_log_trans(root);
  3537. }
  3538. /* see comments for btrfs_del_dir_entries_in_log */
  3539. void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  3540. const struct fscrypt_str *name,
  3541. struct btrfs_inode *inode,
  3542. struct btrfs_inode *dir)
  3543. {
  3544. struct btrfs_root *root = dir->root;
  3545. int ret;
  3546. ret = inode_logged(trans, inode, NULL);
  3547. if (ret == 0)
  3548. return;
  3549. else if (ret < 0) {
  3550. btrfs_set_log_full_commit(trans);
  3551. return;
  3552. }
  3553. ret = join_running_log_trans(root);
  3554. ASSERT(ret == 0, "join_running_log_trans() ret=%d", ret);
  3555. if (WARN_ON(ret))
  3556. return;
  3557. mutex_lock(&inode->log_mutex);
  3558. ret = btrfs_del_inode_ref(trans, root->log_root, name, btrfs_ino(inode),
  3559. btrfs_ino(dir), NULL);
  3560. mutex_unlock(&inode->log_mutex);
  3561. if (ret < 0 && ret != -ENOENT)
  3562. btrfs_set_log_full_commit(trans);
  3563. btrfs_end_log_trans(root);
  3564. }
  3565. /*
  3566. * creates a range item in the log for 'dirid'. first_offset and
  3567. * last_offset tell us which parts of the key space the log should
  3568. * be considered authoritative for.
  3569. */
  3570. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  3571. struct btrfs_root *log,
  3572. struct btrfs_path *path,
  3573. u64 dirid,
  3574. u64 first_offset, u64 last_offset)
  3575. {
  3576. int ret;
  3577. struct btrfs_key key;
  3578. struct btrfs_dir_log_item *item;
  3579. key.objectid = dirid;
  3580. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  3581. key.offset = first_offset;
  3582. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  3583. /*
  3584. * -EEXIST is fine and can happen sporadically when we are logging a
  3585. * directory and have concurrent insertions in the subvolume's tree for
  3586. * items from other inodes and that result in pushing off some dir items
  3587. * from one leaf to another in order to accommodate for the new items.
  3588. * This results in logging the same dir index range key.
  3589. */
  3590. if (ret && ret != -EEXIST)
  3591. return ret;
  3592. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3593. struct btrfs_dir_log_item);
  3594. if (ret == -EEXIST) {
  3595. const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item);
  3596. /*
  3597. * btrfs_del_dir_entries_in_log() might have been called during
  3598. * an unlink between the initial insertion of this key and the
  3599. * current update, or we might be logging a single entry deletion
  3600. * during a rename, so set the new last_offset to the max value.
  3601. */
  3602. last_offset = max(last_offset, curr_end);
  3603. }
  3604. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  3605. btrfs_release_path(path);
  3606. return 0;
  3607. }
  3608. static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
  3609. struct btrfs_inode *inode,
  3610. struct extent_buffer *src,
  3611. struct btrfs_path *dst_path,
  3612. int start_slot,
  3613. int count)
  3614. {
  3615. struct btrfs_root *log = inode->root->log_root;
  3616. char AUTO_KFREE(ins_data);
  3617. struct btrfs_item_batch batch;
  3618. struct extent_buffer *dst;
  3619. unsigned long src_offset;
  3620. unsigned long dst_offset;
  3621. u64 last_index;
  3622. struct btrfs_key key;
  3623. u32 item_size;
  3624. int ret;
  3625. int i;
  3626. ASSERT(count > 0, "count=%d", count);
  3627. batch.nr = count;
  3628. if (count == 1) {
  3629. btrfs_item_key_to_cpu(src, &key, start_slot);
  3630. item_size = btrfs_item_size(src, start_slot);
  3631. batch.keys = &key;
  3632. batch.data_sizes = &item_size;
  3633. batch.total_data_size = item_size;
  3634. } else {
  3635. struct btrfs_key *ins_keys;
  3636. u32 *ins_sizes;
  3637. ins_data = kmalloc_array(count, sizeof(u32) + sizeof(struct btrfs_key), GFP_NOFS);
  3638. if (!ins_data)
  3639. return -ENOMEM;
  3640. ins_sizes = (u32 *)ins_data;
  3641. ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
  3642. batch.keys = ins_keys;
  3643. batch.data_sizes = ins_sizes;
  3644. batch.total_data_size = 0;
  3645. for (i = 0; i < count; i++) {
  3646. const int slot = start_slot + i;
  3647. btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
  3648. ins_sizes[i] = btrfs_item_size(src, slot);
  3649. batch.total_data_size += ins_sizes[i];
  3650. }
  3651. }
  3652. ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
  3653. if (ret)
  3654. return ret;
  3655. dst = dst_path->nodes[0];
  3656. /*
  3657. * Copy all the items in bulk, in a single copy operation. Item data is
  3658. * organized such that it's placed at the end of a leaf and from right
  3659. * to left. For example, the data for the second item ends at an offset
  3660. * that matches the offset where the data for the first item starts, the
  3661. * data for the third item ends at an offset that matches the offset
  3662. * where the data of the second items starts, and so on.
  3663. * Therefore our source and destination start offsets for copy match the
  3664. * offsets of the last items (highest slots).
  3665. */
  3666. dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
  3667. src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
  3668. copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
  3669. btrfs_release_path(dst_path);
  3670. last_index = batch.keys[count - 1].offset;
  3671. ASSERT(last_index > inode->last_dir_index_offset,
  3672. "last_index=%llu inode->last_dir_index_offset=%llu",
  3673. last_index, inode->last_dir_index_offset);
  3674. /*
  3675. * If for some unexpected reason the last item's index is not greater
  3676. * than the last index we logged, warn and force a transaction commit.
  3677. */
  3678. if (WARN_ON(last_index <= inode->last_dir_index_offset))
  3679. ret = BTRFS_LOG_FORCE_COMMIT;
  3680. else
  3681. inode->last_dir_index_offset = last_index;
  3682. if (btrfs_get_first_dir_index_to_log(inode) == 0)
  3683. btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset);
  3684. return ret;
  3685. }
  3686. static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx)
  3687. {
  3688. const int slot = path->slots[0];
  3689. if (ctx->scratch_eb) {
  3690. copy_extent_buffer_full(ctx->scratch_eb, path->nodes[0]);
  3691. } else {
  3692. ctx->scratch_eb = btrfs_clone_extent_buffer(path->nodes[0]);
  3693. if (!ctx->scratch_eb)
  3694. return -ENOMEM;
  3695. }
  3696. btrfs_release_path(path);
  3697. path->nodes[0] = ctx->scratch_eb;
  3698. path->slots[0] = slot;
  3699. /*
  3700. * Add extra ref to scratch eb so that it is not freed when callers
  3701. * release the path, so we can reuse it later if needed.
  3702. */
  3703. refcount_inc(&ctx->scratch_eb->refs);
  3704. return 0;
  3705. }
  3706. static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
  3707. struct btrfs_inode *inode,
  3708. struct btrfs_path *path,
  3709. struct btrfs_path *dst_path,
  3710. struct btrfs_log_ctx *ctx,
  3711. u64 *last_old_dentry_offset)
  3712. {
  3713. struct btrfs_root *log = inode->root->log_root;
  3714. struct extent_buffer *src;
  3715. const int nritems = btrfs_header_nritems(path->nodes[0]);
  3716. const u64 ino = btrfs_ino(inode);
  3717. bool last_found = false;
  3718. int batch_start = 0;
  3719. int batch_size = 0;
  3720. int ret;
  3721. /*
  3722. * We need to clone the leaf, release the read lock on it, and use the
  3723. * clone before modifying the log tree. See the comment at copy_items()
  3724. * about why we need to do this.
  3725. */
  3726. ret = clone_leaf(path, ctx);
  3727. if (ret < 0)
  3728. return ret;
  3729. src = path->nodes[0];
  3730. for (int i = path->slots[0]; i < nritems; i++) {
  3731. struct btrfs_dir_item *di;
  3732. struct btrfs_key key;
  3733. btrfs_item_key_to_cpu(src, &key, i);
  3734. if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) {
  3735. last_found = true;
  3736. break;
  3737. }
  3738. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3739. /*
  3740. * Skip ranges of items that consist only of dir item keys created
  3741. * in past transactions. However if we find a gap, we must log a
  3742. * dir index range item for that gap, so that index keys in that
  3743. * gap are deleted during log replay.
  3744. */
  3745. if (btrfs_dir_transid(src, di) < trans->transid) {
  3746. if (key.offset > *last_old_dentry_offset + 1) {
  3747. ret = insert_dir_log_key(trans, log, dst_path,
  3748. ino, *last_old_dentry_offset + 1,
  3749. key.offset - 1);
  3750. if (ret < 0)
  3751. return ret;
  3752. }
  3753. *last_old_dentry_offset = key.offset;
  3754. continue;
  3755. }
  3756. /* If we logged this dir index item before, we can skip it. */
  3757. if (key.offset <= inode->last_dir_index_offset)
  3758. continue;
  3759. /*
  3760. * We must make sure that when we log a directory entry, the
  3761. * corresponding inode, after log replay, has a matching link
  3762. * count. For example:
  3763. *
  3764. * touch foo
  3765. * mkdir mydir
  3766. * sync
  3767. * ln foo mydir/bar
  3768. * xfs_io -c "fsync" mydir
  3769. * <crash>
  3770. * <mount fs and log replay>
  3771. *
  3772. * Would result in a fsync log that when replayed, our file inode
  3773. * would have a link count of 1, but we get two directory entries
  3774. * pointing to the same inode. After removing one of the names,
  3775. * it would not be possible to remove the other name, which
  3776. * resulted always in stale file handle errors, and would not be
  3777. * possible to rmdir the parent directory, since its i_size could
  3778. * never be decremented to the value BTRFS_EMPTY_DIR_SIZE,
  3779. * resulting in -ENOTEMPTY errors.
  3780. */
  3781. if (!ctx->log_new_dentries) {
  3782. struct btrfs_key di_key;
  3783. btrfs_dir_item_key_to_cpu(src, di, &di_key);
  3784. if (di_key.type != BTRFS_ROOT_ITEM_KEY)
  3785. ctx->log_new_dentries = true;
  3786. }
  3787. if (batch_size == 0)
  3788. batch_start = i;
  3789. batch_size++;
  3790. }
  3791. if (batch_size > 0) {
  3792. ret = flush_dir_items_batch(trans, inode, src, dst_path,
  3793. batch_start, batch_size);
  3794. if (ret < 0)
  3795. return ret;
  3796. }
  3797. return last_found ? 1 : 0;
  3798. }
  3799. /*
  3800. * log all the items included in the current transaction for a given
  3801. * directory. This also creates the range items in the log tree required
  3802. * to replay anything deleted before the fsync
  3803. */
  3804. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  3805. struct btrfs_inode *inode,
  3806. struct btrfs_path *path,
  3807. struct btrfs_path *dst_path,
  3808. struct btrfs_log_ctx *ctx,
  3809. u64 min_offset, u64 *last_offset_ret)
  3810. {
  3811. struct btrfs_key min_key;
  3812. struct btrfs_root *root = inode->root;
  3813. struct btrfs_root *log = root->log_root;
  3814. int ret;
  3815. u64 last_old_dentry_offset = min_offset - 1;
  3816. u64 last_offset = (u64)-1;
  3817. u64 ino = btrfs_ino(inode);
  3818. min_key.objectid = ino;
  3819. min_key.type = BTRFS_DIR_INDEX_KEY;
  3820. min_key.offset = min_offset;
  3821. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  3822. /*
  3823. * we didn't find anything from this transaction, see if there
  3824. * is anything at all
  3825. */
  3826. if (ret != 0 || min_key.objectid != ino ||
  3827. min_key.type != BTRFS_DIR_INDEX_KEY) {
  3828. min_key.objectid = ino;
  3829. min_key.type = BTRFS_DIR_INDEX_KEY;
  3830. min_key.offset = (u64)-1;
  3831. btrfs_release_path(path);
  3832. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3833. if (ret < 0) {
  3834. btrfs_release_path(path);
  3835. return ret;
  3836. }
  3837. ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
  3838. /* if ret == 0 there are items for this type,
  3839. * create a range to tell us the last key of this type.
  3840. * otherwise, there are no items in this directory after
  3841. * *min_offset, and we create a range to indicate that.
  3842. */
  3843. if (ret == 0) {
  3844. struct btrfs_key tmp;
  3845. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  3846. path->slots[0]);
  3847. if (tmp.type == BTRFS_DIR_INDEX_KEY)
  3848. last_old_dentry_offset = tmp.offset;
  3849. } else if (ret > 0) {
  3850. ret = 0;
  3851. }
  3852. goto done;
  3853. }
  3854. /* go backward to find any previous key */
  3855. ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
  3856. if (ret == 0) {
  3857. struct btrfs_key tmp;
  3858. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3859. /*
  3860. * The dir index key before the first one we found that needs to
  3861. * be logged might be in a previous leaf, and there might be a
  3862. * gap between these keys, meaning that we had deletions that
  3863. * happened. So the key range item we log (key type
  3864. * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the
  3865. * previous key's offset plus 1, so that those deletes are replayed.
  3866. */
  3867. if (tmp.type == BTRFS_DIR_INDEX_KEY)
  3868. last_old_dentry_offset = tmp.offset;
  3869. } else if (ret < 0) {
  3870. goto done;
  3871. }
  3872. btrfs_release_path(path);
  3873. /*
  3874. * Find the first key from this transaction again or the one we were at
  3875. * in the loop below in case we had to reschedule. We may be logging the
  3876. * directory without holding its VFS lock, which happen when logging new
  3877. * dentries (through log_new_dir_dentries()) or in some cases when we
  3878. * need to log the parent directory of an inode. This means a dir index
  3879. * key might be deleted from the inode's root, and therefore we may not
  3880. * find it anymore. If we can't find it, just move to the next key. We
  3881. * can not bail out and ignore, because if we do that we will simply
  3882. * not log dir index keys that come after the one that was just deleted
  3883. * and we can end up logging a dir index range that ends at (u64)-1
  3884. * (@last_offset is initialized to that), resulting in removing dir
  3885. * entries we should not remove at log replay time.
  3886. */
  3887. search:
  3888. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3889. if (ret > 0) {
  3890. ret = btrfs_next_item(root, path);
  3891. if (ret > 0) {
  3892. /* There are no more keys in the inode's root. */
  3893. ret = 0;
  3894. goto done;
  3895. }
  3896. }
  3897. if (ret < 0)
  3898. goto done;
  3899. /*
  3900. * we have a block from this transaction, log every item in it
  3901. * from our directory
  3902. */
  3903. while (1) {
  3904. ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx,
  3905. &last_old_dentry_offset);
  3906. if (ret != 0) {
  3907. if (ret > 0)
  3908. ret = 0;
  3909. goto done;
  3910. }
  3911. path->slots[0] = btrfs_header_nritems(path->nodes[0]);
  3912. /*
  3913. * look ahead to the next item and see if it is also
  3914. * from this directory and from this transaction
  3915. */
  3916. ret = btrfs_next_leaf(root, path);
  3917. if (ret) {
  3918. if (ret == 1) {
  3919. last_offset = (u64)-1;
  3920. ret = 0;
  3921. }
  3922. goto done;
  3923. }
  3924. btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
  3925. if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) {
  3926. last_offset = (u64)-1;
  3927. goto done;
  3928. }
  3929. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3930. /*
  3931. * The next leaf was not changed in the current transaction
  3932. * and has at least one dir index key.
  3933. * We check for the next key because there might have been
  3934. * one or more deletions between the last key we logged and
  3935. * that next key. So the key range item we log (key type
  3936. * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's
  3937. * offset minus 1, so that those deletes are replayed.
  3938. */
  3939. last_offset = min_key.offset - 1;
  3940. goto done;
  3941. }
  3942. if (need_resched()) {
  3943. btrfs_release_path(path);
  3944. cond_resched();
  3945. goto search;
  3946. }
  3947. }
  3948. done:
  3949. btrfs_release_path(path);
  3950. btrfs_release_path(dst_path);
  3951. if (ret == 0) {
  3952. *last_offset_ret = last_offset;
  3953. /*
  3954. * In case the leaf was changed in the current transaction but
  3955. * all its dir items are from a past transaction, the last item
  3956. * in the leaf is a dir item and there's no gap between that last
  3957. * dir item and the first one on the next leaf (which did not
  3958. * change in the current transaction), then we don't need to log
  3959. * a range, last_old_dentry_offset is == to last_offset.
  3960. */
  3961. ASSERT(last_old_dentry_offset <= last_offset,
  3962. "last_old_dentry_offset=%llu last_offset=%llu",
  3963. last_old_dentry_offset, last_offset);
  3964. if (last_old_dentry_offset < last_offset)
  3965. ret = insert_dir_log_key(trans, log, path, ino,
  3966. last_old_dentry_offset + 1,
  3967. last_offset);
  3968. }
  3969. return ret;
  3970. }
  3971. /*
  3972. * If the inode was logged before and it was evicted, then its
  3973. * last_dir_index_offset is 0, so we don't know the value of the last index
  3974. * key offset. If that's the case, search for it and update the inode. This
  3975. * is to avoid lookups in the log tree every time we try to insert a dir index
  3976. * key from a leaf changed in the current transaction, and to allow us to always
  3977. * do batch insertions of dir index keys.
  3978. */
  3979. static int update_last_dir_index_offset(struct btrfs_inode *inode,
  3980. struct btrfs_path *path,
  3981. const struct btrfs_log_ctx *ctx)
  3982. {
  3983. const u64 ino = btrfs_ino(inode);
  3984. struct btrfs_key key;
  3985. int ret;
  3986. lockdep_assert_held(&inode->log_mutex);
  3987. if (inode->last_dir_index_offset != 0)
  3988. return 0;
  3989. if (!ctx->logged_before) {
  3990. inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
  3991. return 0;
  3992. }
  3993. key.objectid = ino;
  3994. key.type = BTRFS_DIR_INDEX_KEY;
  3995. key.offset = (u64)-1;
  3996. ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
  3997. /*
  3998. * An error happened or we actually have an index key with an offset
  3999. * value of (u64)-1. Bail out, we're done.
  4000. */
  4001. if (ret <= 0)
  4002. goto out;
  4003. ret = 0;
  4004. inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
  4005. /*
  4006. * No dir index items, bail out and leave last_dir_index_offset with
  4007. * the value right before the first valid index value.
  4008. */
  4009. if (path->slots[0] == 0)
  4010. goto out;
  4011. /*
  4012. * btrfs_search_slot() left us at one slot beyond the slot with the last
  4013. * index key, or beyond the last key of the directory that is not an
  4014. * index key. If we have an index key before, set last_dir_index_offset
  4015. * to its offset value, otherwise leave it with a value right before the
  4016. * first valid index value, as it means we have an empty directory.
  4017. */
  4018. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
  4019. if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY)
  4020. inode->last_dir_index_offset = key.offset;
  4021. out:
  4022. btrfs_release_path(path);
  4023. return ret;
  4024. }
  4025. /*
  4026. * logging directories is very similar to logging inodes, We find all the items
  4027. * from the current transaction and write them to the log.
  4028. *
  4029. * The recovery code scans the directory in the subvolume, and if it finds a
  4030. * key in the range logged that is not present in the log tree, then it means
  4031. * that dir entry was unlinked during the transaction.
  4032. *
  4033. * In order for that scan to work, we must include one key smaller than
  4034. * the smallest logged by this transaction and one key larger than the largest
  4035. * key logged by this transaction.
  4036. */
  4037. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  4038. struct btrfs_inode *inode,
  4039. struct btrfs_path *path,
  4040. struct btrfs_path *dst_path,
  4041. struct btrfs_log_ctx *ctx)
  4042. {
  4043. u64 min_key;
  4044. u64 max_key;
  4045. int ret;
  4046. ret = update_last_dir_index_offset(inode, path, ctx);
  4047. if (ret)
  4048. return ret;
  4049. min_key = BTRFS_DIR_START_INDEX;
  4050. max_key = 0;
  4051. while (1) {
  4052. ret = log_dir_items(trans, inode, path, dst_path,
  4053. ctx, min_key, &max_key);
  4054. if (ret)
  4055. return ret;
  4056. if (max_key == (u64)-1)
  4057. break;
  4058. min_key = max_key + 1;
  4059. }
  4060. return 0;
  4061. }
  4062. /*
  4063. * a helper function to drop items from the log before we relog an
  4064. * inode. max_key_type indicates the highest item type to remove.
  4065. * This cannot be run for file data extents because it does not
  4066. * free the extents they point to.
  4067. */
  4068. static int drop_inode_items(struct btrfs_trans_handle *trans,
  4069. struct btrfs_root *log,
  4070. struct btrfs_path *path,
  4071. struct btrfs_inode *inode,
  4072. int max_key_type)
  4073. {
  4074. int ret;
  4075. struct btrfs_key key;
  4076. struct btrfs_key found_key;
  4077. int start_slot;
  4078. key.objectid = btrfs_ino(inode);
  4079. key.type = max_key_type;
  4080. key.offset = (u64)-1;
  4081. while (1) {
  4082. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  4083. if (ret < 0) {
  4084. break;
  4085. } else if (ret > 0) {
  4086. if (path->slots[0] == 0)
  4087. break;
  4088. path->slots[0]--;
  4089. }
  4090. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  4091. path->slots[0]);
  4092. if (found_key.objectid != key.objectid)
  4093. break;
  4094. found_key.offset = 0;
  4095. found_key.type = 0;
  4096. ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot);
  4097. if (ret < 0)
  4098. break;
  4099. ret = btrfs_del_items(trans, log, path, start_slot,
  4100. path->slots[0] - start_slot + 1);
  4101. /*
  4102. * If start slot isn't 0 then we don't need to re-search, we've
  4103. * found the last guy with the objectid in this tree.
  4104. */
  4105. if (ret || start_slot != 0)
  4106. break;
  4107. btrfs_release_path(path);
  4108. }
  4109. btrfs_release_path(path);
  4110. if (ret > 0)
  4111. ret = 0;
  4112. return ret;
  4113. }
  4114. static int truncate_inode_items(struct btrfs_trans_handle *trans,
  4115. struct btrfs_root *log_root,
  4116. struct btrfs_inode *inode,
  4117. u64 new_size, u32 min_type)
  4118. {
  4119. struct btrfs_truncate_control control = {
  4120. .new_size = new_size,
  4121. .ino = btrfs_ino(inode),
  4122. .min_type = min_type,
  4123. .skip_ref_updates = true,
  4124. };
  4125. return btrfs_truncate_inode_items(trans, log_root, &control);
  4126. }
  4127. static void fill_inode_item(struct btrfs_trans_handle *trans,
  4128. struct extent_buffer *leaf,
  4129. struct btrfs_inode_item *item,
  4130. struct inode *inode, bool log_inode_only,
  4131. u64 logged_isize)
  4132. {
  4133. u64 gen = BTRFS_I(inode)->generation;
  4134. u64 flags;
  4135. if (log_inode_only) {
  4136. /*
  4137. * Set the generation to zero so the recover code can tell the
  4138. * difference between a logging just to say 'this inode exists'
  4139. * and a logging to say 'update this inode with these values'.
  4140. * But only if the inode was not already logged before.
  4141. * We access ->logged_trans directly since it was already set
  4142. * up in the call chain by btrfs_log_inode(), and data_race()
  4143. * to avoid false alerts from KCSAN and since it was set already
  4144. * and one can set it to 0 since that only happens on eviction
  4145. * and we are holding a ref on the inode.
  4146. */
  4147. ASSERT(data_race(BTRFS_I(inode)->logged_trans) > 0);
  4148. if (data_race(BTRFS_I(inode)->logged_trans) < trans->transid)
  4149. gen = 0;
  4150. btrfs_set_inode_size(leaf, item, logged_isize);
  4151. } else {
  4152. btrfs_set_inode_size(leaf, item, inode->i_size);
  4153. }
  4154. btrfs_set_inode_generation(leaf, item, gen);
  4155. btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
  4156. btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
  4157. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  4158. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  4159. btrfs_set_timespec_sec(leaf, &item->atime, inode_get_atime_sec(inode));
  4160. btrfs_set_timespec_nsec(leaf, &item->atime, inode_get_atime_nsec(inode));
  4161. btrfs_set_timespec_sec(leaf, &item->mtime, inode_get_mtime_sec(inode));
  4162. btrfs_set_timespec_nsec(leaf, &item->mtime, inode_get_mtime_nsec(inode));
  4163. btrfs_set_timespec_sec(leaf, &item->ctime, inode_get_ctime_sec(inode));
  4164. btrfs_set_timespec_nsec(leaf, &item->ctime, inode_get_ctime_nsec(inode));
  4165. btrfs_set_timespec_sec(leaf, &item->otime, BTRFS_I(inode)->i_otime_sec);
  4166. btrfs_set_timespec_nsec(leaf, &item->otime, BTRFS_I(inode)->i_otime_nsec);
  4167. /*
  4168. * We do not need to set the nbytes field, in fact during a fast fsync
  4169. * its value may not even be correct, since a fast fsync does not wait
  4170. * for ordered extent completion, which is where we update nbytes, it
  4171. * only waits for writeback to complete. During log replay as we find
  4172. * file extent items and replay them, we adjust the nbytes field of the
  4173. * inode item in subvolume tree as needed (see overwrite_item()).
  4174. */
  4175. btrfs_set_inode_sequence(leaf, item, inode_peek_iversion(inode));
  4176. btrfs_set_inode_transid(leaf, item, trans->transid);
  4177. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  4178. flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
  4179. BTRFS_I(inode)->ro_flags);
  4180. btrfs_set_inode_flags(leaf, item, flags);
  4181. btrfs_set_inode_block_group(leaf, item, 0);
  4182. }
  4183. static int log_inode_item(struct btrfs_trans_handle *trans,
  4184. struct btrfs_root *log, struct btrfs_path *path,
  4185. struct btrfs_inode *inode, bool inode_item_dropped)
  4186. {
  4187. struct btrfs_inode_item *inode_item;
  4188. struct btrfs_key key;
  4189. int ret;
  4190. btrfs_get_inode_key(inode, &key);
  4191. /*
  4192. * If we are doing a fast fsync and the inode was logged before in the
  4193. * current transaction, then we know the inode was previously logged and
  4194. * it exists in the log tree. For performance reasons, in this case use
  4195. * btrfs_search_slot() directly with ins_len set to 0 so that we never
  4196. * attempt a write lock on the leaf's parent, which adds unnecessary lock
  4197. * contention in case there are concurrent fsyncs for other inodes of the
  4198. * same subvolume. Using btrfs_insert_empty_item() when the inode item
  4199. * already exists can also result in unnecessarily splitting a leaf.
  4200. */
  4201. if (!inode_item_dropped && inode->logged_trans == trans->transid) {
  4202. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  4203. ASSERT(ret <= 0);
  4204. if (ret > 0)
  4205. ret = -ENOENT;
  4206. } else {
  4207. /*
  4208. * This means it is the first fsync in the current transaction,
  4209. * so the inode item is not in the log and we need to insert it.
  4210. * We can never get -EEXIST because we are only called for a fast
  4211. * fsync and in case an inode eviction happens after the inode was
  4212. * logged before in the current transaction, when we load again
  4213. * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
  4214. * flags and set ->logged_trans to 0.
  4215. */
  4216. ret = btrfs_insert_empty_item(trans, log, path, &key,
  4217. sizeof(*inode_item));
  4218. ASSERT(ret != -EEXIST);
  4219. }
  4220. if (ret)
  4221. return ret;
  4222. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4223. struct btrfs_inode_item);
  4224. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  4225. false, 0);
  4226. btrfs_release_path(path);
  4227. return 0;
  4228. }
  4229. static int log_csums(struct btrfs_trans_handle *trans,
  4230. struct btrfs_inode *inode,
  4231. struct btrfs_root *log_root,
  4232. struct btrfs_ordered_sum *sums)
  4233. {
  4234. const u64 lock_end = sums->logical + sums->len - 1;
  4235. struct extent_state *cached_state = NULL;
  4236. int ret;
  4237. /*
  4238. * If this inode was not used for reflink operations in the current
  4239. * transaction with new extents, then do the fast path, no need to
  4240. * worry about logging checksum items with overlapping ranges.
  4241. */
  4242. if (inode->last_reflink_trans < trans->transid)
  4243. return btrfs_csum_file_blocks(trans, log_root, sums);
  4244. /*
  4245. * Serialize logging for checksums. This is to avoid racing with the
  4246. * same checksum being logged by another task that is logging another
  4247. * file which happens to refer to the same extent as well. Such races
  4248. * can leave checksum items in the log with overlapping ranges.
  4249. */
  4250. ret = btrfs_lock_extent(&log_root->log_csum_range, sums->logical, lock_end,
  4251. &cached_state);
  4252. if (ret)
  4253. return ret;
  4254. /*
  4255. * Due to extent cloning, we might have logged a csum item that covers a
  4256. * subrange of a cloned extent, and later we can end up logging a csum
  4257. * item for a larger subrange of the same extent or the entire range.
  4258. * This would leave csum items in the log tree that cover the same range
  4259. * and break the searches for checksums in the log tree, resulting in
  4260. * some checksums missing in the fs/subvolume tree. So just delete (or
  4261. * trim and adjust) any existing csum items in the log for this range.
  4262. */
  4263. ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len);
  4264. if (!ret)
  4265. ret = btrfs_csum_file_blocks(trans, log_root, sums);
  4266. btrfs_unlock_extent(&log_root->log_csum_range, sums->logical, lock_end,
  4267. &cached_state);
  4268. return ret;
  4269. }
  4270. static noinline int copy_items(struct btrfs_trans_handle *trans,
  4271. struct btrfs_inode *inode,
  4272. struct btrfs_path *dst_path,
  4273. struct btrfs_path *src_path,
  4274. int start_slot, int nr, int inode_only,
  4275. u64 logged_isize, struct btrfs_log_ctx *ctx)
  4276. {
  4277. struct btrfs_root *log = inode->root->log_root;
  4278. struct btrfs_file_extent_item *extent;
  4279. struct extent_buffer *src;
  4280. int ret;
  4281. struct btrfs_key *ins_keys;
  4282. u32 *ins_sizes;
  4283. struct btrfs_item_batch batch;
  4284. char AUTO_KFREE(ins_data);
  4285. int dst_index;
  4286. const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM);
  4287. const u64 i_size = i_size_read(&inode->vfs_inode);
  4288. /*
  4289. * To keep lockdep happy and avoid deadlocks, clone the source leaf and
  4290. * use the clone. This is because otherwise we would be changing the log
  4291. * tree, to insert items from the subvolume tree or insert csum items,
  4292. * while holding a read lock on a leaf from the subvolume tree, which
  4293. * creates a nasty lock dependency when COWing log tree nodes/leaves:
  4294. *
  4295. * 1) Modifying the log tree triggers an extent buffer allocation while
  4296. * holding a write lock on a parent extent buffer from the log tree.
  4297. * Allocating the pages for an extent buffer, or the extent buffer
  4298. * struct, can trigger inode eviction and finally the inode eviction
  4299. * will trigger a release/remove of a delayed node, which requires
  4300. * taking the delayed node's mutex;
  4301. *
  4302. * 2) Allocating a metadata extent for a log tree can trigger the async
  4303. * reclaim thread and make us wait for it to release enough space and
  4304. * unblock our reservation ticket. The reclaim thread can start
  4305. * flushing delayed items, and that in turn results in the need to
  4306. * lock delayed node mutexes and in the need to write lock extent
  4307. * buffers of a subvolume tree - all this while holding a write lock
  4308. * on the parent extent buffer in the log tree.
  4309. *
  4310. * So one task in scenario 1) running in parallel with another task in
  4311. * scenario 2) could lead to a deadlock, one wanting to lock a delayed
  4312. * node mutex while having a read lock on a leaf from the subvolume,
  4313. * while the other is holding the delayed node's mutex and wants to
  4314. * write lock the same subvolume leaf for flushing delayed items.
  4315. */
  4316. ret = clone_leaf(src_path, ctx);
  4317. if (ret < 0)
  4318. return ret;
  4319. src = src_path->nodes[0];
  4320. ins_data = kmalloc_array(nr, sizeof(struct btrfs_key) + sizeof(u32), GFP_NOFS);
  4321. if (!ins_data)
  4322. return -ENOMEM;
  4323. ins_sizes = (u32 *)ins_data;
  4324. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  4325. batch.keys = ins_keys;
  4326. batch.data_sizes = ins_sizes;
  4327. batch.total_data_size = 0;
  4328. batch.nr = 0;
  4329. dst_index = 0;
  4330. for (int i = 0; i < nr; i++) {
  4331. const int src_slot = start_slot + i;
  4332. struct btrfs_root *csum_root;
  4333. struct btrfs_ordered_sum *sums;
  4334. struct btrfs_ordered_sum *sums_next;
  4335. LIST_HEAD(ordered_sums);
  4336. u64 disk_bytenr;
  4337. u64 disk_num_bytes;
  4338. u64 extent_offset;
  4339. u64 extent_num_bytes;
  4340. bool is_old_extent;
  4341. btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot);
  4342. if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY)
  4343. goto add_to_batch;
  4344. extent = btrfs_item_ptr(src, src_slot,
  4345. struct btrfs_file_extent_item);
  4346. is_old_extent = (btrfs_file_extent_generation(src, extent) <
  4347. trans->transid);
  4348. /*
  4349. * Don't copy extents from past generations. That would make us
  4350. * log a lot more metadata for common cases like doing only a
  4351. * few random writes into a file and then fsync it for the first
  4352. * time or after the full sync flag is set on the inode. We can
  4353. * get leaves full of extent items, most of which are from past
  4354. * generations, so we can skip them - as long as the inode has
  4355. * not been the target of a reflink operation in this transaction,
  4356. * as in that case it might have had file extent items with old
  4357. * generations copied into it. We also must always log prealloc
  4358. * extents that start at or beyond eof, otherwise we would lose
  4359. * them on log replay.
  4360. */
  4361. if (is_old_extent &&
  4362. ins_keys[dst_index].offset < i_size &&
  4363. inode->last_reflink_trans < trans->transid)
  4364. continue;
  4365. if (skip_csum)
  4366. goto add_to_batch;
  4367. /* Only regular extents have checksums. */
  4368. if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG)
  4369. goto add_to_batch;
  4370. /*
  4371. * If it's an extent created in a past transaction, then its
  4372. * checksums are already accessible from the committed csum tree,
  4373. * no need to log them.
  4374. */
  4375. if (is_old_extent)
  4376. goto add_to_batch;
  4377. disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent);
  4378. /* If it's an explicit hole, there are no checksums. */
  4379. if (disk_bytenr == 0)
  4380. goto add_to_batch;
  4381. disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent);
  4382. if (btrfs_file_extent_compression(src, extent)) {
  4383. extent_offset = 0;
  4384. extent_num_bytes = disk_num_bytes;
  4385. } else {
  4386. extent_offset = btrfs_file_extent_offset(src, extent);
  4387. extent_num_bytes = btrfs_file_extent_num_bytes(src, extent);
  4388. }
  4389. csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr);
  4390. if (unlikely(!csum_root)) {
  4391. btrfs_err(trans->fs_info,
  4392. "missing csum root for extent at bytenr %llu",
  4393. disk_bytenr);
  4394. return -EUCLEAN;
  4395. }
  4396. disk_bytenr += extent_offset;
  4397. ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
  4398. disk_bytenr + extent_num_bytes - 1,
  4399. &ordered_sums, false);
  4400. if (ret < 0)
  4401. return ret;
  4402. ret = 0;
  4403. list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) {
  4404. if (!ret)
  4405. ret = log_csums(trans, inode, log, sums);
  4406. list_del(&sums->list);
  4407. kfree(sums);
  4408. }
  4409. if (ret)
  4410. return ret;
  4411. add_to_batch:
  4412. ins_sizes[dst_index] = btrfs_item_size(src, src_slot);
  4413. batch.total_data_size += ins_sizes[dst_index];
  4414. batch.nr++;
  4415. dst_index++;
  4416. }
  4417. /*
  4418. * We have a leaf full of old extent items that don't need to be logged,
  4419. * so we don't need to do anything.
  4420. */
  4421. if (batch.nr == 0)
  4422. return 0;
  4423. ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
  4424. if (ret)
  4425. return ret;
  4426. dst_index = 0;
  4427. for (int i = 0; i < nr; i++) {
  4428. const int src_slot = start_slot + i;
  4429. const int dst_slot = dst_path->slots[0] + dst_index;
  4430. struct btrfs_key key;
  4431. unsigned long src_offset;
  4432. unsigned long dst_offset;
  4433. /*
  4434. * We're done, all the remaining items in the source leaf
  4435. * correspond to old file extent items.
  4436. */
  4437. if (dst_index >= batch.nr)
  4438. break;
  4439. btrfs_item_key_to_cpu(src, &key, src_slot);
  4440. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4441. goto copy_item;
  4442. extent = btrfs_item_ptr(src, src_slot,
  4443. struct btrfs_file_extent_item);
  4444. /* See the comment in the previous loop, same logic. */
  4445. if (btrfs_file_extent_generation(src, extent) < trans->transid &&
  4446. key.offset < i_size &&
  4447. inode->last_reflink_trans < trans->transid)
  4448. continue;
  4449. copy_item:
  4450. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot);
  4451. src_offset = btrfs_item_ptr_offset(src, src_slot);
  4452. if (key.type == BTRFS_INODE_ITEM_KEY) {
  4453. struct btrfs_inode_item *inode_item;
  4454. inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot,
  4455. struct btrfs_inode_item);
  4456. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  4457. &inode->vfs_inode,
  4458. inode_only == LOG_INODE_EXISTS,
  4459. logged_isize);
  4460. } else {
  4461. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  4462. src_offset, ins_sizes[dst_index]);
  4463. }
  4464. dst_index++;
  4465. }
  4466. btrfs_release_path(dst_path);
  4467. return ret;
  4468. }
  4469. static int extent_cmp(void *priv, const struct list_head *a,
  4470. const struct list_head *b)
  4471. {
  4472. const struct extent_map *em1, *em2;
  4473. em1 = list_entry(a, struct extent_map, list);
  4474. em2 = list_entry(b, struct extent_map, list);
  4475. if (em1->start < em2->start)
  4476. return -1;
  4477. else if (em1->start > em2->start)
  4478. return 1;
  4479. return 0;
  4480. }
  4481. static int log_extent_csums(struct btrfs_trans_handle *trans,
  4482. struct btrfs_inode *inode,
  4483. struct btrfs_root *log_root,
  4484. const struct extent_map *em,
  4485. struct btrfs_log_ctx *ctx)
  4486. {
  4487. struct btrfs_ordered_extent *ordered;
  4488. struct btrfs_root *csum_root;
  4489. u64 block_start;
  4490. u64 csum_offset;
  4491. u64 csum_len;
  4492. u64 mod_start = em->start;
  4493. u64 mod_len = em->len;
  4494. LIST_HEAD(ordered_sums);
  4495. int ret = 0;
  4496. if (inode->flags & BTRFS_INODE_NODATASUM ||
  4497. (em->flags & EXTENT_FLAG_PREALLOC) ||
  4498. em->disk_bytenr == EXTENT_MAP_HOLE)
  4499. return 0;
  4500. list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
  4501. const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
  4502. const u64 mod_end = mod_start + mod_len;
  4503. struct btrfs_ordered_sum *sums;
  4504. if (mod_len == 0)
  4505. break;
  4506. if (ordered_end <= mod_start)
  4507. continue;
  4508. if (mod_end <= ordered->file_offset)
  4509. break;
  4510. /*
  4511. * We are going to copy all the csums on this ordered extent, so
  4512. * go ahead and adjust mod_start and mod_len in case this ordered
  4513. * extent has already been logged.
  4514. */
  4515. if (ordered->file_offset > mod_start) {
  4516. if (ordered_end >= mod_end)
  4517. mod_len = ordered->file_offset - mod_start;
  4518. /*
  4519. * If we have this case
  4520. *
  4521. * |--------- logged extent ---------|
  4522. * |----- ordered extent ----|
  4523. *
  4524. * Just don't mess with mod_start and mod_len, we'll
  4525. * just end up logging more csums than we need and it
  4526. * will be ok.
  4527. */
  4528. } else {
  4529. if (ordered_end < mod_end) {
  4530. mod_len = mod_end - ordered_end;
  4531. mod_start = ordered_end;
  4532. } else {
  4533. mod_len = 0;
  4534. }
  4535. }
  4536. /*
  4537. * To keep us from looping for the above case of an ordered
  4538. * extent that falls inside of the logged extent.
  4539. */
  4540. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
  4541. continue;
  4542. list_for_each_entry(sums, &ordered->list, list) {
  4543. ret = log_csums(trans, inode, log_root, sums);
  4544. if (ret)
  4545. return ret;
  4546. }
  4547. }
  4548. /* We're done, found all csums in the ordered extents. */
  4549. if (mod_len == 0)
  4550. return 0;
  4551. /* If we're compressed we have to save the entire range of csums. */
  4552. if (btrfs_extent_map_is_compressed(em)) {
  4553. csum_offset = 0;
  4554. csum_len = em->disk_num_bytes;
  4555. } else {
  4556. csum_offset = mod_start - em->start;
  4557. csum_len = mod_len;
  4558. }
  4559. /* block start is already adjusted for the file extent offset. */
  4560. block_start = btrfs_extent_map_block_start(em);
  4561. csum_root = btrfs_csum_root(trans->fs_info, block_start);
  4562. if (unlikely(!csum_root)) {
  4563. btrfs_err(trans->fs_info,
  4564. "missing csum root for extent at bytenr %llu",
  4565. block_start);
  4566. return -EUCLEAN;
  4567. }
  4568. ret = btrfs_lookup_csums_list(csum_root, block_start + csum_offset,
  4569. block_start + csum_offset + csum_len - 1,
  4570. &ordered_sums, false);
  4571. if (ret < 0)
  4572. return ret;
  4573. ret = 0;
  4574. while (!list_empty(&ordered_sums)) {
  4575. struct btrfs_ordered_sum *sums = list_first_entry(&ordered_sums,
  4576. struct btrfs_ordered_sum,
  4577. list);
  4578. if (!ret)
  4579. ret = log_csums(trans, inode, log_root, sums);
  4580. list_del(&sums->list);
  4581. kfree(sums);
  4582. }
  4583. return ret;
  4584. }
  4585. static int log_one_extent(struct btrfs_trans_handle *trans,
  4586. struct btrfs_inode *inode,
  4587. const struct extent_map *em,
  4588. struct btrfs_path *path,
  4589. struct btrfs_log_ctx *ctx)
  4590. {
  4591. struct btrfs_drop_extents_args drop_args = { 0 };
  4592. struct btrfs_root *log = inode->root->log_root;
  4593. struct btrfs_file_extent_item fi = { 0 };
  4594. struct extent_buffer *leaf;
  4595. struct btrfs_key key;
  4596. enum btrfs_compression_type compress_type;
  4597. u64 extent_offset = em->offset;
  4598. u64 block_start = btrfs_extent_map_block_start(em);
  4599. u64 block_len;
  4600. int ret;
  4601. btrfs_set_stack_file_extent_generation(&fi, trans->transid);
  4602. if (em->flags & EXTENT_FLAG_PREALLOC)
  4603. btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
  4604. else
  4605. btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
  4606. block_len = em->disk_num_bytes;
  4607. compress_type = btrfs_extent_map_compression(em);
  4608. if (compress_type != BTRFS_COMPRESS_NONE) {
  4609. btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start);
  4610. btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
  4611. } else if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
  4612. btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start - extent_offset);
  4613. btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
  4614. }
  4615. btrfs_set_stack_file_extent_offset(&fi, extent_offset);
  4616. btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
  4617. btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);
  4618. btrfs_set_stack_file_extent_compression(&fi, compress_type);
  4619. ret = log_extent_csums(trans, inode, log, em, ctx);
  4620. if (ret)
  4621. return ret;
  4622. /*
  4623. * If this is the first time we are logging the inode in the current
  4624. * transaction, we can avoid btrfs_drop_extents(), which is expensive
  4625. * because it does a deletion search, which always acquires write locks
  4626. * for extent buffers at levels 2, 1 and 0. This not only wastes time
  4627. * but also adds significant contention in a log tree, since log trees
  4628. * are small, with a root at level 2 or 3 at most, due to their short
  4629. * life span.
  4630. */
  4631. if (ctx->logged_before) {
  4632. drop_args.path = path;
  4633. drop_args.start = em->start;
  4634. drop_args.end = btrfs_extent_map_end(em);
  4635. drop_args.replace_extent = true;
  4636. drop_args.extent_item_size = sizeof(fi);
  4637. ret = btrfs_drop_extents(trans, log, inode, &drop_args);
  4638. if (ret)
  4639. return ret;
  4640. }
  4641. if (!drop_args.extent_inserted) {
  4642. key.objectid = btrfs_ino(inode);
  4643. key.type = BTRFS_EXTENT_DATA_KEY;
  4644. key.offset = em->start;
  4645. ret = btrfs_insert_empty_item(trans, log, path, &key,
  4646. sizeof(fi));
  4647. if (ret)
  4648. return ret;
  4649. }
  4650. leaf = path->nodes[0];
  4651. write_extent_buffer(leaf, &fi,
  4652. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4653. sizeof(fi));
  4654. btrfs_release_path(path);
  4655. return ret;
  4656. }
  4657. /*
  4658. * Log all prealloc extents beyond the inode's i_size to make sure we do not
  4659. * lose them after doing a full/fast fsync and replaying the log. We scan the
  4660. * subvolume's root instead of iterating the inode's extent map tree because
  4661. * otherwise we can log incorrect extent items based on extent map conversion.
  4662. * That can happen due to the fact that extent maps are merged when they
  4663. * are not in the extent map tree's list of modified extents.
  4664. */
  4665. static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
  4666. struct btrfs_inode *inode,
  4667. struct btrfs_path *path,
  4668. struct btrfs_log_ctx *ctx)
  4669. {
  4670. struct btrfs_root *root = inode->root;
  4671. struct btrfs_key key;
  4672. const u64 i_size = i_size_read(&inode->vfs_inode);
  4673. const u64 ino = btrfs_ino(inode);
  4674. BTRFS_PATH_AUTO_FREE(dst_path);
  4675. bool dropped_extents = false;
  4676. u64 truncate_offset = i_size;
  4677. struct extent_buffer *leaf;
  4678. int slot;
  4679. int ins_nr = 0;
  4680. int start_slot = 0;
  4681. int ret;
  4682. if (!(inode->flags & BTRFS_INODE_PREALLOC))
  4683. return 0;
  4684. key.objectid = ino;
  4685. key.type = BTRFS_EXTENT_DATA_KEY;
  4686. key.offset = i_size;
  4687. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4688. if (ret < 0)
  4689. goto out;
  4690. /*
  4691. * We must check if there is a prealloc extent that starts before the
  4692. * i_size and crosses the i_size boundary. This is to ensure later we
  4693. * truncate down to the end of that extent and not to the i_size, as
  4694. * otherwise we end up losing part of the prealloc extent after a log
  4695. * replay and with an implicit hole if there is another prealloc extent
  4696. * that starts at an offset beyond i_size.
  4697. */
  4698. ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
  4699. if (ret < 0)
  4700. goto out;
  4701. if (ret == 0) {
  4702. struct btrfs_file_extent_item *ei;
  4703. leaf = path->nodes[0];
  4704. slot = path->slots[0];
  4705. ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4706. if (btrfs_file_extent_type(leaf, ei) ==
  4707. BTRFS_FILE_EXTENT_PREALLOC) {
  4708. u64 extent_end;
  4709. btrfs_item_key_to_cpu(leaf, &key, slot);
  4710. extent_end = key.offset +
  4711. btrfs_file_extent_num_bytes(leaf, ei);
  4712. if (extent_end > i_size)
  4713. truncate_offset = extent_end;
  4714. }
  4715. } else {
  4716. ret = 0;
  4717. }
  4718. while (true) {
  4719. leaf = path->nodes[0];
  4720. slot = path->slots[0];
  4721. if (slot >= btrfs_header_nritems(leaf)) {
  4722. if (ins_nr > 0) {
  4723. ret = copy_items(trans, inode, dst_path, path,
  4724. start_slot, ins_nr, 1, 0, ctx);
  4725. if (ret < 0)
  4726. goto out;
  4727. ins_nr = 0;
  4728. }
  4729. ret = btrfs_next_leaf(root, path);
  4730. if (ret < 0)
  4731. goto out;
  4732. if (ret > 0) {
  4733. ret = 0;
  4734. break;
  4735. }
  4736. continue;
  4737. }
  4738. btrfs_item_key_to_cpu(leaf, &key, slot);
  4739. if (key.objectid > ino)
  4740. break;
  4741. if (WARN_ON_ONCE(key.objectid < ino) ||
  4742. key.type < BTRFS_EXTENT_DATA_KEY ||
  4743. key.offset < i_size) {
  4744. path->slots[0]++;
  4745. continue;
  4746. }
  4747. /*
  4748. * Avoid overlapping items in the log tree. The first time we
  4749. * get here, get rid of everything from a past fsync. After
  4750. * that, if the current extent starts before the end of the last
  4751. * extent we copied, truncate the last one. This can happen if
  4752. * an ordered extent completion modifies the subvolume tree
  4753. * while btrfs_next_leaf() has the tree unlocked.
  4754. */
  4755. if (!dropped_extents || key.offset < truncate_offset) {
  4756. ret = truncate_inode_items(trans, root->log_root, inode,
  4757. min(key.offset, truncate_offset),
  4758. BTRFS_EXTENT_DATA_KEY);
  4759. if (ret)
  4760. goto out;
  4761. dropped_extents = true;
  4762. }
  4763. truncate_offset = btrfs_file_extent_end(path);
  4764. if (ins_nr == 0)
  4765. start_slot = slot;
  4766. ins_nr++;
  4767. path->slots[0]++;
  4768. if (!dst_path) {
  4769. dst_path = btrfs_alloc_path();
  4770. if (!dst_path) {
  4771. ret = -ENOMEM;
  4772. goto out;
  4773. }
  4774. }
  4775. }
  4776. if (ins_nr > 0)
  4777. ret = copy_items(trans, inode, dst_path, path,
  4778. start_slot, ins_nr, 1, 0, ctx);
  4779. out:
  4780. btrfs_release_path(path);
  4781. return ret;
  4782. }
  4783. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  4784. struct btrfs_inode *inode,
  4785. struct btrfs_path *path,
  4786. struct btrfs_log_ctx *ctx)
  4787. {
  4788. struct btrfs_ordered_extent *ordered;
  4789. struct btrfs_ordered_extent *tmp;
  4790. struct extent_map *em, *n;
  4791. LIST_HEAD(extents);
  4792. struct extent_map_tree *tree = &inode->extent_tree;
  4793. int ret = 0;
  4794. int num = 0;
  4795. write_lock(&tree->lock);
  4796. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  4797. list_del_init(&em->list);
  4798. /*
  4799. * Just an arbitrary number, this can be really CPU intensive
  4800. * once we start getting a lot of extents, and really once we
  4801. * have a bunch of extents we just want to commit since it will
  4802. * be faster.
  4803. */
  4804. if (++num > 32768) {
  4805. list_del_init(&tree->modified_extents);
  4806. ret = -EFBIG;
  4807. goto process;
  4808. }
  4809. if (em->generation < trans->transid)
  4810. continue;
  4811. /* We log prealloc extents beyond eof later. */
  4812. if ((em->flags & EXTENT_FLAG_PREALLOC) &&
  4813. em->start >= i_size_read(&inode->vfs_inode))
  4814. continue;
  4815. /* Need a ref to keep it from getting evicted from cache */
  4816. refcount_inc(&em->refs);
  4817. em->flags |= EXTENT_FLAG_LOGGING;
  4818. list_add_tail(&em->list, &extents);
  4819. num++;
  4820. }
  4821. list_sort(NULL, &extents, extent_cmp);
  4822. process:
  4823. while (!list_empty(&extents)) {
  4824. em = list_first_entry(&extents, struct extent_map, list);
  4825. list_del_init(&em->list);
  4826. /*
  4827. * If we had an error we just need to delete everybody from our
  4828. * private list.
  4829. */
  4830. if (ret) {
  4831. btrfs_clear_em_logging(inode, em);
  4832. btrfs_free_extent_map(em);
  4833. continue;
  4834. }
  4835. write_unlock(&tree->lock);
  4836. ret = log_one_extent(trans, inode, em, path, ctx);
  4837. write_lock(&tree->lock);
  4838. btrfs_clear_em_logging(inode, em);
  4839. btrfs_free_extent_map(em);
  4840. }
  4841. WARN_ON(!list_empty(&extents));
  4842. write_unlock(&tree->lock);
  4843. if (!ret)
  4844. ret = btrfs_log_prealloc_extents(trans, inode, path, ctx);
  4845. if (ret)
  4846. return ret;
  4847. /*
  4848. * We have logged all extents successfully, now make sure the commit of
  4849. * the current transaction waits for the ordered extents to complete
  4850. * before it commits and wipes out the log trees, otherwise we would
  4851. * lose data if an ordered extents completes after the transaction
  4852. * commits and a power failure happens after the transaction commit.
  4853. */
  4854. list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
  4855. list_del_init(&ordered->log_list);
  4856. set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
  4857. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  4858. spin_lock(&inode->ordered_tree_lock);
  4859. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  4860. set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
  4861. atomic_inc(&trans->transaction->pending_ordered);
  4862. }
  4863. spin_unlock(&inode->ordered_tree_lock);
  4864. }
  4865. btrfs_put_ordered_extent(ordered);
  4866. }
  4867. return 0;
  4868. }
  4869. static int get_inode_size_to_log(struct btrfs_trans_handle *trans,
  4870. struct btrfs_inode *inode,
  4871. struct btrfs_path *path, u64 *size_ret)
  4872. {
  4873. struct btrfs_key key;
  4874. struct btrfs_inode_item *item;
  4875. int ret;
  4876. key.objectid = btrfs_ino(inode);
  4877. key.type = BTRFS_INODE_ITEM_KEY;
  4878. key.offset = 0;
  4879. /*
  4880. * Our caller called inode_logged(), so logged_trans is up to date.
  4881. * Use data_race() to silence any warning from KCSAN. Once logged_trans
  4882. * is set, it can only be reset to 0 after inode eviction.
  4883. */
  4884. if (data_race(inode->logged_trans) == trans->transid) {
  4885. ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
  4886. } else if (inode->generation < trans->transid) {
  4887. path->search_commit_root = true;
  4888. path->skip_locking = true;
  4889. ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
  4890. path->search_commit_root = false;
  4891. path->skip_locking = false;
  4892. } else {
  4893. *size_ret = 0;
  4894. return 0;
  4895. }
  4896. /*
  4897. * If the inode was logged before or is from a past transaction, then
  4898. * its inode item must exist in the log root or in the commit root.
  4899. */
  4900. ASSERT(ret <= 0);
  4901. if (WARN_ON_ONCE(ret > 0))
  4902. ret = -ENOENT;
  4903. if (ret < 0)
  4904. return ret;
  4905. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4906. struct btrfs_inode_item);
  4907. *size_ret = btrfs_inode_size(path->nodes[0], item);
  4908. /*
  4909. * If the in-memory inode's i_size is smaller then the inode size stored
  4910. * in the btree, return the inode's i_size, so that we get a correct
  4911. * inode size after replaying the log when before a power failure we had
  4912. * a shrinking truncate followed by addition of a new name (rename / new
  4913. * hard link). Otherwise return the inode size from the btree, to avoid
  4914. * data loss when replaying a log due to previously doing a write that
  4915. * expands the inode's size and logging a new name immediately after.
  4916. */
  4917. if (*size_ret > inode->vfs_inode.i_size)
  4918. *size_ret = inode->vfs_inode.i_size;
  4919. btrfs_release_path(path);
  4920. return 0;
  4921. }
  4922. /*
  4923. * At the moment we always log all xattrs. This is to figure out at log replay
  4924. * time which xattrs must have their deletion replayed. If a xattr is missing
  4925. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  4926. * because if a xattr is deleted, the inode is fsynced and a power failure
  4927. * happens, causing the log to be replayed the next time the fs is mounted,
  4928. * we want the xattr to not exist anymore (same behaviour as other filesystems
  4929. * with a journal, ext3/4, xfs, f2fs, etc).
  4930. */
  4931. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  4932. struct btrfs_inode *inode,
  4933. struct btrfs_path *path,
  4934. struct btrfs_path *dst_path,
  4935. struct btrfs_log_ctx *ctx)
  4936. {
  4937. struct btrfs_root *root = inode->root;
  4938. int ret;
  4939. struct btrfs_key key;
  4940. const u64 ino = btrfs_ino(inode);
  4941. int ins_nr = 0;
  4942. int start_slot = 0;
  4943. bool found_xattrs = false;
  4944. if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
  4945. return 0;
  4946. key.objectid = ino;
  4947. key.type = BTRFS_XATTR_ITEM_KEY;
  4948. key.offset = 0;
  4949. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4950. if (ret < 0)
  4951. return ret;
  4952. while (true) {
  4953. int slot = path->slots[0];
  4954. struct extent_buffer *leaf = path->nodes[0];
  4955. int nritems = btrfs_header_nritems(leaf);
  4956. if (slot >= nritems) {
  4957. if (ins_nr > 0) {
  4958. ret = copy_items(trans, inode, dst_path, path,
  4959. start_slot, ins_nr, 1, 0, ctx);
  4960. if (ret < 0)
  4961. return ret;
  4962. ins_nr = 0;
  4963. }
  4964. ret = btrfs_next_leaf(root, path);
  4965. if (ret < 0)
  4966. return ret;
  4967. else if (ret > 0)
  4968. break;
  4969. continue;
  4970. }
  4971. btrfs_item_key_to_cpu(leaf, &key, slot);
  4972. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  4973. break;
  4974. if (ins_nr == 0)
  4975. start_slot = slot;
  4976. ins_nr++;
  4977. path->slots[0]++;
  4978. found_xattrs = true;
  4979. cond_resched();
  4980. }
  4981. if (ins_nr > 0) {
  4982. ret = copy_items(trans, inode, dst_path, path,
  4983. start_slot, ins_nr, 1, 0, ctx);
  4984. if (ret < 0)
  4985. return ret;
  4986. }
  4987. if (!found_xattrs)
  4988. set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
  4989. return 0;
  4990. }
  4991. /*
  4992. * When using the NO_HOLES feature if we punched a hole that causes the
  4993. * deletion of entire leafs or all the extent items of the first leaf (the one
  4994. * that contains the inode item and references) we may end up not processing
  4995. * any extents, because there are no leafs with a generation matching the
  4996. * current transaction that have extent items for our inode. So we need to find
  4997. * if any holes exist and then log them. We also need to log holes after any
  4998. * truncate operation that changes the inode's size.
  4999. */
  5000. static int btrfs_log_holes(struct btrfs_trans_handle *trans,
  5001. struct btrfs_inode *inode,
  5002. struct btrfs_path *path)
  5003. {
  5004. struct btrfs_root *root = inode->root;
  5005. struct btrfs_fs_info *fs_info = root->fs_info;
  5006. struct btrfs_key key;
  5007. const u64 ino = btrfs_ino(inode);
  5008. const u64 i_size = i_size_read(&inode->vfs_inode);
  5009. u64 prev_extent_end = 0;
  5010. int ret;
  5011. if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
  5012. return 0;
  5013. key.objectid = ino;
  5014. key.type = BTRFS_EXTENT_DATA_KEY;
  5015. key.offset = 0;
  5016. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5017. if (ret < 0)
  5018. return ret;
  5019. while (true) {
  5020. struct extent_buffer *leaf = path->nodes[0];
  5021. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  5022. ret = btrfs_next_leaf(root, path);
  5023. if (ret < 0)
  5024. return ret;
  5025. if (ret > 0) {
  5026. ret = 0;
  5027. break;
  5028. }
  5029. leaf = path->nodes[0];
  5030. }
  5031. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  5032. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
  5033. break;
  5034. /* We have a hole, log it. */
  5035. if (prev_extent_end < key.offset) {
  5036. const u64 hole_len = key.offset - prev_extent_end;
  5037. /*
  5038. * Release the path to avoid deadlocks with other code
  5039. * paths that search the root while holding locks on
  5040. * leafs from the log root.
  5041. */
  5042. btrfs_release_path(path);
  5043. ret = btrfs_insert_hole_extent(trans, root->log_root,
  5044. ino, prev_extent_end,
  5045. hole_len);
  5046. if (ret < 0)
  5047. return ret;
  5048. /*
  5049. * Search for the same key again in the root. Since it's
  5050. * an extent item and we are holding the inode lock, the
  5051. * key must still exist. If it doesn't just emit warning
  5052. * and return an error to fall back to a transaction
  5053. * commit.
  5054. */
  5055. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5056. if (ret < 0)
  5057. return ret;
  5058. if (WARN_ON(ret > 0))
  5059. return -ENOENT;
  5060. leaf = path->nodes[0];
  5061. }
  5062. prev_extent_end = btrfs_file_extent_end(path);
  5063. path->slots[0]++;
  5064. cond_resched();
  5065. }
  5066. if (prev_extent_end < i_size) {
  5067. u64 hole_len;
  5068. btrfs_release_path(path);
  5069. hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
  5070. ret = btrfs_insert_hole_extent(trans, root->log_root, ino,
  5071. prev_extent_end, hole_len);
  5072. if (ret < 0)
  5073. return ret;
  5074. }
  5075. return 0;
  5076. }
  5077. /*
  5078. * When we are logging a new inode X, check if it doesn't have a reference that
  5079. * matches the reference from some other inode Y created in a past transaction
  5080. * and that was renamed in the current transaction. If we don't do this, then at
  5081. * log replay time we can lose inode Y (and all its files if it's a directory):
  5082. *
  5083. * mkdir /mnt/x
  5084. * echo "hello world" > /mnt/x/foobar
  5085. * sync
  5086. * mv /mnt/x /mnt/y
  5087. * mkdir /mnt/x # or touch /mnt/x
  5088. * xfs_io -c fsync /mnt/x
  5089. * <power fail>
  5090. * mount fs, trigger log replay
  5091. *
  5092. * After the log replay procedure, we would lose the first directory and all its
  5093. * files (file foobar).
  5094. * For the case where inode Y is not a directory we simply end up losing it:
  5095. *
  5096. * echo "123" > /mnt/foo
  5097. * sync
  5098. * mv /mnt/foo /mnt/bar
  5099. * echo "abc" > /mnt/foo
  5100. * xfs_io -c fsync /mnt/foo
  5101. * <power fail>
  5102. *
  5103. * We also need this for cases where a snapshot entry is replaced by some other
  5104. * entry (file or directory) otherwise we end up with an unreplayable log due to
  5105. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  5106. * if it were a regular entry:
  5107. *
  5108. * mkdir /mnt/x
  5109. * btrfs subvolume snapshot /mnt /mnt/x/snap
  5110. * btrfs subvolume delete /mnt/x/snap
  5111. * rmdir /mnt/x
  5112. * mkdir /mnt/x
  5113. * fsync /mnt/x or fsync some new file inside it
  5114. * <power fail>
  5115. *
  5116. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  5117. * the same transaction.
  5118. */
  5119. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  5120. const int slot,
  5121. const struct btrfs_key *key,
  5122. struct btrfs_inode *inode,
  5123. u64 *other_ino, u64 *other_parent)
  5124. {
  5125. BTRFS_PATH_AUTO_FREE(search_path);
  5126. char AUTO_KFREE(name);
  5127. u32 name_len = 0;
  5128. u32 item_size = btrfs_item_size(eb, slot);
  5129. u32 cur_offset = 0;
  5130. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  5131. search_path = btrfs_alloc_path();
  5132. if (!search_path)
  5133. return -ENOMEM;
  5134. search_path->search_commit_root = true;
  5135. search_path->skip_locking = true;
  5136. while (cur_offset < item_size) {
  5137. u64 parent;
  5138. u32 this_name_len;
  5139. u32 this_len;
  5140. unsigned long name_ptr;
  5141. struct btrfs_dir_item *di;
  5142. struct fscrypt_str name_str;
  5143. if (key->type == BTRFS_INODE_REF_KEY) {
  5144. struct btrfs_inode_ref *iref;
  5145. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  5146. parent = key->offset;
  5147. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  5148. name_ptr = (unsigned long)(iref + 1);
  5149. this_len = sizeof(*iref) + this_name_len;
  5150. } else {
  5151. struct btrfs_inode_extref *extref;
  5152. extref = (struct btrfs_inode_extref *)(ptr +
  5153. cur_offset);
  5154. parent = btrfs_inode_extref_parent(eb, extref);
  5155. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  5156. name_ptr = (unsigned long)&extref->name;
  5157. this_len = sizeof(*extref) + this_name_len;
  5158. }
  5159. if (this_name_len > name_len) {
  5160. char *new_name;
  5161. new_name = krealloc(name, this_name_len, GFP_NOFS);
  5162. if (!new_name)
  5163. return -ENOMEM;
  5164. name_len = this_name_len;
  5165. name = new_name;
  5166. }
  5167. read_extent_buffer(eb, name, name_ptr, this_name_len);
  5168. name_str.name = name;
  5169. name_str.len = this_name_len;
  5170. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  5171. parent, &name_str, 0);
  5172. if (di && !IS_ERR(di)) {
  5173. struct btrfs_key di_key;
  5174. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  5175. di, &di_key);
  5176. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  5177. if (di_key.objectid != key->objectid) {
  5178. *other_ino = di_key.objectid;
  5179. *other_parent = parent;
  5180. return 1;
  5181. } else {
  5182. return 0;
  5183. }
  5184. } else {
  5185. return -EAGAIN;
  5186. }
  5187. } else if (IS_ERR(di)) {
  5188. return PTR_ERR(di);
  5189. }
  5190. btrfs_release_path(search_path);
  5191. cur_offset += this_len;
  5192. }
  5193. return 0;
  5194. }
  5195. /*
  5196. * Check if we need to log an inode. This is used in contexts where while
  5197. * logging an inode we need to log another inode (either that it exists or in
  5198. * full mode). This is used instead of btrfs_inode_in_log() because the later
  5199. * requires the inode to be in the log and have the log transaction committed,
  5200. * while here we do not care if the log transaction was already committed - our
  5201. * caller will commit the log later - and we want to avoid logging an inode
  5202. * multiple times when multiple tasks have joined the same log transaction.
  5203. */
  5204. static bool need_log_inode(const struct btrfs_trans_handle *trans,
  5205. struct btrfs_inode *inode)
  5206. {
  5207. /*
  5208. * If a directory was not modified, no dentries added or removed, we can
  5209. * and should avoid logging it.
  5210. */
  5211. if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
  5212. return false;
  5213. /*
  5214. * If this inode does not have new/updated/deleted xattrs since the last
  5215. * time it was logged and is flagged as logged in the current transaction,
  5216. * we can skip logging it. As for new/deleted names, those are updated in
  5217. * the log by link/unlink/rename operations.
  5218. * In case the inode was logged and then evicted and reloaded, its
  5219. * logged_trans will be 0, in which case we have to fully log it since
  5220. * logged_trans is a transient field, not persisted.
  5221. */
  5222. if (inode_logged(trans, inode, NULL) == 1 &&
  5223. !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
  5224. return false;
  5225. return true;
  5226. }
  5227. struct btrfs_dir_list {
  5228. u64 ino;
  5229. struct list_head list;
  5230. };
  5231. /*
  5232. * Log the inodes of the new dentries of a directory.
  5233. * See process_dir_items_leaf() for details about why it is needed.
  5234. * This is a recursive operation - if an existing dentry corresponds to a
  5235. * directory, that directory's new entries are logged too (same behaviour as
  5236. * ext3/4, xfs, f2fs, nilfs2). Note that when logging the inodes
  5237. * the dentries point to we do not acquire their VFS lock, otherwise lockdep
  5238. * complains about the following circular lock dependency / possible deadlock:
  5239. *
  5240. * CPU0 CPU1
  5241. * ---- ----
  5242. * lock(&type->i_mutex_dir_key#3/2);
  5243. * lock(sb_internal#2);
  5244. * lock(&type->i_mutex_dir_key#3/2);
  5245. * lock(&sb->s_type->i_mutex_key#14);
  5246. *
  5247. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  5248. * sb_start_intwrite() in btrfs_start_transaction().
  5249. * Not acquiring the VFS lock of the inodes is still safe because:
  5250. *
  5251. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  5252. * that while logging the inode new references (names) are added or removed
  5253. * from the inode, leaving the logged inode item with a link count that does
  5254. * not match the number of logged inode reference items. This is fine because
  5255. * at log replay time we compute the real number of links and correct the
  5256. * link count in the inode item (see replay_one_buffer() and
  5257. * link_to_fixup_dir());
  5258. *
  5259. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  5260. * while logging the inode's items new index items (key type
  5261. * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item
  5262. * has a size that doesn't match the sum of the lengths of all the logged
  5263. * names - this is ok, not a problem, because at log replay time we set the
  5264. * directory's i_size to the correct value (see replay_one_name() and
  5265. * overwrite_item()).
  5266. */
  5267. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  5268. struct btrfs_inode *start_inode,
  5269. struct btrfs_log_ctx *ctx)
  5270. {
  5271. struct btrfs_root *root = start_inode->root;
  5272. struct btrfs_path *path;
  5273. LIST_HEAD(dir_list);
  5274. struct btrfs_dir_list *dir_elem;
  5275. u64 ino = btrfs_ino(start_inode);
  5276. struct btrfs_inode *curr_inode = start_inode;
  5277. int ret = 0;
  5278. path = btrfs_alloc_path();
  5279. if (!path)
  5280. return -ENOMEM;
  5281. /* Pairs with btrfs_add_delayed_iput below. */
  5282. ihold(&curr_inode->vfs_inode);
  5283. while (true) {
  5284. struct btrfs_key key;
  5285. struct btrfs_key found_key;
  5286. u64 next_index;
  5287. bool continue_curr_inode = true;
  5288. int iter_ret;
  5289. key.objectid = ino;
  5290. key.type = BTRFS_DIR_INDEX_KEY;
  5291. key.offset = btrfs_get_first_dir_index_to_log(curr_inode);
  5292. next_index = key.offset;
  5293. again:
  5294. btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) {
  5295. struct extent_buffer *leaf = path->nodes[0];
  5296. struct btrfs_dir_item *di;
  5297. struct btrfs_key di_key;
  5298. struct btrfs_inode *di_inode;
  5299. int log_mode = LOG_INODE_EXISTS;
  5300. int type;
  5301. if (found_key.objectid != ino ||
  5302. found_key.type != BTRFS_DIR_INDEX_KEY) {
  5303. continue_curr_inode = false;
  5304. break;
  5305. }
  5306. next_index = found_key.offset + 1;
  5307. di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
  5308. type = btrfs_dir_ftype(leaf, di);
  5309. if (btrfs_dir_transid(leaf, di) < trans->transid)
  5310. continue;
  5311. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  5312. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  5313. continue;
  5314. btrfs_release_path(path);
  5315. di_inode = btrfs_iget_logging(di_key.objectid, root);
  5316. if (IS_ERR(di_inode)) {
  5317. ret = PTR_ERR(di_inode);
  5318. goto out;
  5319. }
  5320. if (!need_log_inode(trans, di_inode)) {
  5321. btrfs_add_delayed_iput(di_inode);
  5322. break;
  5323. }
  5324. ctx->log_new_dentries = false;
  5325. if (type == BTRFS_FT_DIR)
  5326. log_mode = LOG_INODE_ALL;
  5327. ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
  5328. btrfs_add_delayed_iput(di_inode);
  5329. if (ret)
  5330. goto out;
  5331. if (ctx->log_new_dentries) {
  5332. dir_elem = kmalloc_obj(*dir_elem, GFP_NOFS);
  5333. if (!dir_elem) {
  5334. ret = -ENOMEM;
  5335. goto out;
  5336. }
  5337. dir_elem->ino = di_key.objectid;
  5338. list_add_tail(&dir_elem->list, &dir_list);
  5339. }
  5340. break;
  5341. }
  5342. btrfs_release_path(path);
  5343. if (iter_ret < 0) {
  5344. ret = iter_ret;
  5345. goto out;
  5346. } else if (iter_ret > 0) {
  5347. continue_curr_inode = false;
  5348. } else {
  5349. key = found_key;
  5350. }
  5351. if (continue_curr_inode && key.offset < (u64)-1) {
  5352. key.offset++;
  5353. goto again;
  5354. }
  5355. btrfs_set_first_dir_index_to_log(curr_inode, next_index);
  5356. if (list_empty(&dir_list))
  5357. break;
  5358. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list);
  5359. ino = dir_elem->ino;
  5360. list_del(&dir_elem->list);
  5361. kfree(dir_elem);
  5362. btrfs_add_delayed_iput(curr_inode);
  5363. curr_inode = btrfs_iget_logging(ino, root);
  5364. if (IS_ERR(curr_inode)) {
  5365. ret = PTR_ERR(curr_inode);
  5366. curr_inode = NULL;
  5367. break;
  5368. }
  5369. }
  5370. out:
  5371. btrfs_free_path(path);
  5372. if (curr_inode)
  5373. btrfs_add_delayed_iput(curr_inode);
  5374. if (ret) {
  5375. struct btrfs_dir_list *next;
  5376. list_for_each_entry_safe(dir_elem, next, &dir_list, list)
  5377. kfree(dir_elem);
  5378. }
  5379. return ret;
  5380. }
  5381. struct btrfs_ino_list {
  5382. u64 ino;
  5383. u64 parent;
  5384. struct list_head list;
  5385. };
  5386. static void free_conflicting_inodes(struct btrfs_log_ctx *ctx)
  5387. {
  5388. struct btrfs_ino_list *curr;
  5389. struct btrfs_ino_list *next;
  5390. list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) {
  5391. list_del(&curr->list);
  5392. kfree(curr);
  5393. }
  5394. }
  5395. static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino,
  5396. struct btrfs_path *path)
  5397. {
  5398. struct btrfs_key key;
  5399. int ret;
  5400. key.objectid = ino;
  5401. key.type = BTRFS_INODE_ITEM_KEY;
  5402. key.offset = 0;
  5403. path->search_commit_root = true;
  5404. path->skip_locking = true;
  5405. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5406. if (WARN_ON_ONCE(ret > 0)) {
  5407. /*
  5408. * We have previously found the inode through the commit root
  5409. * so this should not happen. If it does, just error out and
  5410. * fallback to a transaction commit.
  5411. */
  5412. ret = -ENOENT;
  5413. } else if (ret == 0) {
  5414. struct btrfs_inode_item *item;
  5415. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5416. struct btrfs_inode_item);
  5417. if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item)))
  5418. ret = 1;
  5419. }
  5420. btrfs_release_path(path);
  5421. path->search_commit_root = false;
  5422. path->skip_locking = false;
  5423. return ret;
  5424. }
  5425. static bool can_log_conflicting_inode(const struct btrfs_trans_handle *trans,
  5426. const struct btrfs_inode *inode)
  5427. {
  5428. if (!S_ISDIR(inode->vfs_inode.i_mode))
  5429. return true;
  5430. if (inode->last_unlink_trans < trans->transid)
  5431. return true;
  5432. /*
  5433. * If this is a directory and its unlink_trans is not from a past
  5434. * transaction then we must fallback to a transaction commit in order
  5435. * to avoid getting a directory with 2 hard links after log replay.
  5436. *
  5437. * This happens if a directory A is renamed, moved from one parent
  5438. * directory to another one, a new file is created in the old parent
  5439. * directory with the old name of our directory A, the new file is
  5440. * fsynced, then we moved the new file to some other parent directory
  5441. * and fsync again the new file. This results in a log tree where we
  5442. * logged that directory A existed, with the INODE_REF item for the
  5443. * new location but without having logged its old parent inode, so
  5444. * that on log replay we add a new link for the new location but the
  5445. * old link remains, resulting in a link count of 2.
  5446. */
  5447. return false;
  5448. }
  5449. static int add_conflicting_inode(struct btrfs_trans_handle *trans,
  5450. struct btrfs_root *root,
  5451. struct btrfs_path *path,
  5452. u64 ino, u64 parent,
  5453. struct btrfs_log_ctx *ctx)
  5454. {
  5455. struct btrfs_ino_list *ino_elem;
  5456. struct btrfs_inode *inode;
  5457. /*
  5458. * It's rare to have a lot of conflicting inodes, in practice it is not
  5459. * common to have more than 1 or 2. We don't want to collect too many,
  5460. * as we could end up logging too many inodes (even if only in
  5461. * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction
  5462. * commits.
  5463. */
  5464. if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES)
  5465. return BTRFS_LOG_FORCE_COMMIT;
  5466. inode = btrfs_iget_logging(ino, root);
  5467. /*
  5468. * If the other inode that had a conflicting dir entry was deleted in
  5469. * the current transaction then we either:
  5470. *
  5471. * 1) Log the parent directory (later after adding it to the list) if
  5472. * the inode is a directory. This is because it may be a deleted
  5473. * subvolume/snapshot or it may be a regular directory that had
  5474. * deleted subvolumes/snapshots (or subdirectories that had them),
  5475. * and at the moment we can't deal with dropping subvolumes/snapshots
  5476. * during log replay. So we just log the parent, which will result in
  5477. * a fallback to a transaction commit if we are dealing with those
  5478. * cases (last_unlink_trans will match the current transaction);
  5479. *
  5480. * 2) Do nothing if it's not a directory. During log replay we simply
  5481. * unlink the conflicting dentry from the parent directory and then
  5482. * add the dentry for our inode. Like this we can avoid logging the
  5483. * parent directory (and maybe fallback to a transaction commit in
  5484. * case it has a last_unlink_trans == trans->transid, due to moving
  5485. * some inode from it to some other directory).
  5486. */
  5487. if (IS_ERR(inode)) {
  5488. int ret = PTR_ERR(inode);
  5489. if (ret != -ENOENT)
  5490. return ret;
  5491. ret = conflicting_inode_is_dir(root, ino, path);
  5492. /* Not a directory or we got an error. */
  5493. if (ret <= 0)
  5494. return ret;
  5495. /* Conflicting inode is a directory, so we'll log its parent. */
  5496. ino_elem = kmalloc_obj(*ino_elem, GFP_NOFS);
  5497. if (!ino_elem)
  5498. return -ENOMEM;
  5499. ino_elem->ino = ino;
  5500. ino_elem->parent = parent;
  5501. list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
  5502. ctx->num_conflict_inodes++;
  5503. return 0;
  5504. }
  5505. /*
  5506. * If the inode was already logged skip it - otherwise we can hit an
  5507. * infinite loop. Example:
  5508. *
  5509. * From the commit root (previous transaction) we have the following
  5510. * inodes:
  5511. *
  5512. * inode 257 a directory
  5513. * inode 258 with references "zz" and "zz_link" on inode 257
  5514. * inode 259 with reference "a" on inode 257
  5515. *
  5516. * And in the current (uncommitted) transaction we have:
  5517. *
  5518. * inode 257 a directory, unchanged
  5519. * inode 258 with references "a" and "a2" on inode 257
  5520. * inode 259 with reference "zz_link" on inode 257
  5521. * inode 261 with reference "zz" on inode 257
  5522. *
  5523. * When logging inode 261 the following infinite loop could
  5524. * happen if we don't skip already logged inodes:
  5525. *
  5526. * - we detect inode 258 as a conflicting inode, with inode 261
  5527. * on reference "zz", and log it;
  5528. *
  5529. * - we detect inode 259 as a conflicting inode, with inode 258
  5530. * on reference "a", and log it;
  5531. *
  5532. * - we detect inode 258 as a conflicting inode, with inode 259
  5533. * on reference "zz_link", and log it - again! After this we
  5534. * repeat the above steps forever.
  5535. *
  5536. * Here we can use need_log_inode() because we only need to log the
  5537. * inode in LOG_INODE_EXISTS mode and rename operations update the log,
  5538. * so that the log ends up with the new name and without the old name.
  5539. */
  5540. if (!need_log_inode(trans, inode)) {
  5541. btrfs_add_delayed_iput(inode);
  5542. return 0;
  5543. }
  5544. if (!can_log_conflicting_inode(trans, inode)) {
  5545. btrfs_add_delayed_iput(inode);
  5546. return BTRFS_LOG_FORCE_COMMIT;
  5547. }
  5548. btrfs_add_delayed_iput(inode);
  5549. ino_elem = kmalloc_obj(*ino_elem, GFP_NOFS);
  5550. if (!ino_elem)
  5551. return -ENOMEM;
  5552. ino_elem->ino = ino;
  5553. ino_elem->parent = parent;
  5554. list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
  5555. ctx->num_conflict_inodes++;
  5556. return 0;
  5557. }
  5558. static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
  5559. struct btrfs_root *root,
  5560. struct btrfs_log_ctx *ctx)
  5561. {
  5562. const bool orig_log_new_dentries = ctx->log_new_dentries;
  5563. int ret = 0;
  5564. /*
  5565. * Conflicting inodes are logged by the first call to btrfs_log_inode(),
  5566. * otherwise we could have unbounded recursion of btrfs_log_inode()
  5567. * calls. This check guarantees we can have only 1 level of recursion.
  5568. */
  5569. if (ctx->logging_conflict_inodes)
  5570. return 0;
  5571. ctx->logging_conflict_inodes = true;
  5572. /*
  5573. * New conflicting inodes may be found and added to the list while we
  5574. * are logging a conflicting inode, so keep iterating while the list is
  5575. * not empty.
  5576. */
  5577. while (!list_empty(&ctx->conflict_inodes)) {
  5578. struct btrfs_ino_list *curr;
  5579. struct btrfs_inode *inode;
  5580. u64 ino;
  5581. u64 parent;
  5582. curr = list_first_entry(&ctx->conflict_inodes,
  5583. struct btrfs_ino_list, list);
  5584. ino = curr->ino;
  5585. parent = curr->parent;
  5586. list_del(&curr->list);
  5587. kfree(curr);
  5588. inode = btrfs_iget_logging(ino, root);
  5589. /*
  5590. * If the other inode that had a conflicting dir entry was
  5591. * deleted in the current transaction, we need to log its parent
  5592. * directory. See the comment at add_conflicting_inode().
  5593. */
  5594. if (IS_ERR(inode)) {
  5595. ret = PTR_ERR(inode);
  5596. if (ret != -ENOENT)
  5597. break;
  5598. inode = btrfs_iget_logging(parent, root);
  5599. if (IS_ERR(inode)) {
  5600. ret = PTR_ERR(inode);
  5601. break;
  5602. }
  5603. if (!can_log_conflicting_inode(trans, inode)) {
  5604. btrfs_add_delayed_iput(inode);
  5605. ret = BTRFS_LOG_FORCE_COMMIT;
  5606. break;
  5607. }
  5608. /*
  5609. * Always log the directory, we cannot make this
  5610. * conditional on need_log_inode() because the directory
  5611. * might have been logged in LOG_INODE_EXISTS mode or
  5612. * the dir index of the conflicting inode is not in a
  5613. * dir index key range logged for the directory. So we
  5614. * must make sure the deletion is recorded.
  5615. */
  5616. ctx->log_new_dentries = false;
  5617. ret = btrfs_log_inode(trans, inode, LOG_INODE_ALL, ctx);
  5618. if (!ret && ctx->log_new_dentries)
  5619. ret = log_new_dir_dentries(trans, inode, ctx);
  5620. btrfs_add_delayed_iput(inode);
  5621. if (ret)
  5622. break;
  5623. continue;
  5624. }
  5625. /*
  5626. * Here we can use need_log_inode() because we only need to log
  5627. * the inode in LOG_INODE_EXISTS mode and rename operations
  5628. * update the log, so that the log ends up with the new name and
  5629. * without the old name.
  5630. *
  5631. * We did this check at add_conflicting_inode(), but here we do
  5632. * it again because if some other task logged the inode after
  5633. * that, we can avoid doing it again.
  5634. */
  5635. if (!need_log_inode(trans, inode)) {
  5636. btrfs_add_delayed_iput(inode);
  5637. continue;
  5638. }
  5639. /*
  5640. * We are safe logging the other inode without acquiring its
  5641. * lock as long as we log with the LOG_INODE_EXISTS mode. We
  5642. * are safe against concurrent renames of the other inode as
  5643. * well because during a rename we pin the log and update the
  5644. * log with the new name before we unpin it.
  5645. */
  5646. ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
  5647. btrfs_add_delayed_iput(inode);
  5648. if (ret)
  5649. break;
  5650. }
  5651. ctx->log_new_dentries = orig_log_new_dentries;
  5652. ctx->logging_conflict_inodes = false;
  5653. if (ret)
  5654. free_conflicting_inodes(ctx);
  5655. return ret;
  5656. }
  5657. static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
  5658. struct btrfs_inode *inode,
  5659. struct btrfs_key *min_key,
  5660. const struct btrfs_key *max_key,
  5661. struct btrfs_path *path,
  5662. struct btrfs_path *dst_path,
  5663. const u64 logged_isize,
  5664. const int inode_only,
  5665. struct btrfs_log_ctx *ctx,
  5666. bool *need_log_inode_item)
  5667. {
  5668. const u64 i_size = i_size_read(&inode->vfs_inode);
  5669. struct btrfs_root *root = inode->root;
  5670. int ins_start_slot = 0;
  5671. int ins_nr = 0;
  5672. int ret;
  5673. while (1) {
  5674. ret = btrfs_search_forward(root, min_key, path, trans->transid);
  5675. if (ret < 0)
  5676. return ret;
  5677. if (ret > 0) {
  5678. ret = 0;
  5679. break;
  5680. }
  5681. again:
  5682. /* Note, ins_nr might be > 0 here, cleanup outside the loop */
  5683. if (min_key->objectid != max_key->objectid)
  5684. break;
  5685. if (min_key->type > max_key->type)
  5686. break;
  5687. if (min_key->type == BTRFS_INODE_ITEM_KEY) {
  5688. *need_log_inode_item = false;
  5689. } else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
  5690. min_key->offset >= i_size) {
  5691. /*
  5692. * Extents at and beyond eof are logged with
  5693. * btrfs_log_prealloc_extents().
  5694. * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
  5695. * and no keys greater than that, so bail out.
  5696. */
  5697. break;
  5698. } else if (min_key->type == BTRFS_INODE_REF_KEY ||
  5699. min_key->type == BTRFS_INODE_EXTREF_KEY) {
  5700. u64 other_ino = 0;
  5701. u64 other_parent = 0;
  5702. ret = btrfs_check_ref_name_override(path->nodes[0],
  5703. path->slots[0], min_key, inode,
  5704. &other_ino, &other_parent);
  5705. if (ret < 0) {
  5706. return ret;
  5707. } else if (ret > 0 &&
  5708. other_ino != btrfs_ino(ctx->inode)) {
  5709. if (ins_nr > 0) {
  5710. ins_nr++;
  5711. } else {
  5712. ins_nr = 1;
  5713. ins_start_slot = path->slots[0];
  5714. }
  5715. ret = copy_items(trans, inode, dst_path, path,
  5716. ins_start_slot, ins_nr,
  5717. inode_only, logged_isize, ctx);
  5718. if (ret < 0)
  5719. return ret;
  5720. ins_nr = 0;
  5721. btrfs_release_path(path);
  5722. ret = add_conflicting_inode(trans, root, path,
  5723. other_ino,
  5724. other_parent, ctx);
  5725. if (ret)
  5726. return ret;
  5727. goto next_key;
  5728. }
  5729. } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
  5730. /* Skip xattrs, logged later with btrfs_log_all_xattrs() */
  5731. if (ins_nr == 0)
  5732. goto next_slot;
  5733. ret = copy_items(trans, inode, dst_path, path,
  5734. ins_start_slot,
  5735. ins_nr, inode_only, logged_isize, ctx);
  5736. if (ret < 0)
  5737. return ret;
  5738. ins_nr = 0;
  5739. goto next_slot;
  5740. }
  5741. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  5742. ins_nr++;
  5743. goto next_slot;
  5744. } else if (!ins_nr) {
  5745. ins_start_slot = path->slots[0];
  5746. ins_nr = 1;
  5747. goto next_slot;
  5748. }
  5749. ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
  5750. ins_nr, inode_only, logged_isize, ctx);
  5751. if (ret < 0)
  5752. return ret;
  5753. ins_nr = 1;
  5754. ins_start_slot = path->slots[0];
  5755. next_slot:
  5756. path->slots[0]++;
  5757. if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
  5758. btrfs_item_key_to_cpu(path->nodes[0], min_key,
  5759. path->slots[0]);
  5760. goto again;
  5761. }
  5762. if (ins_nr) {
  5763. ret = copy_items(trans, inode, dst_path, path,
  5764. ins_start_slot, ins_nr, inode_only,
  5765. logged_isize, ctx);
  5766. if (ret < 0)
  5767. return ret;
  5768. ins_nr = 0;
  5769. }
  5770. btrfs_release_path(path);
  5771. next_key:
  5772. if (min_key->offset < (u64)-1) {
  5773. min_key->offset++;
  5774. } else if (min_key->type < max_key->type) {
  5775. min_key->type++;
  5776. min_key->offset = 0;
  5777. } else {
  5778. break;
  5779. }
  5780. /*
  5781. * We may process many leaves full of items for our inode, so
  5782. * avoid monopolizing a cpu for too long by rescheduling while
  5783. * not holding locks on any tree.
  5784. */
  5785. cond_resched();
  5786. }
  5787. if (ins_nr) {
  5788. ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
  5789. ins_nr, inode_only, logged_isize, ctx);
  5790. if (ret)
  5791. return ret;
  5792. }
  5793. if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
  5794. /*
  5795. * Release the path because otherwise we might attempt to double
  5796. * lock the same leaf with btrfs_log_prealloc_extents() below.
  5797. */
  5798. btrfs_release_path(path);
  5799. ret = btrfs_log_prealloc_extents(trans, inode, dst_path, ctx);
  5800. }
  5801. return ret;
  5802. }
  5803. static int insert_delayed_items_batch(struct btrfs_trans_handle *trans,
  5804. struct btrfs_root *log,
  5805. struct btrfs_path *path,
  5806. const struct btrfs_item_batch *batch,
  5807. const struct btrfs_delayed_item *first_item)
  5808. {
  5809. const struct btrfs_delayed_item *curr = first_item;
  5810. int ret;
  5811. ret = btrfs_insert_empty_items(trans, log, path, batch);
  5812. if (ret)
  5813. return ret;
  5814. for (int i = 0; i < batch->nr; i++) {
  5815. char *data_ptr;
  5816. data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
  5817. write_extent_buffer(path->nodes[0], &curr->data,
  5818. (unsigned long)data_ptr, curr->data_len);
  5819. curr = list_next_entry(curr, log_list);
  5820. path->slots[0]++;
  5821. }
  5822. btrfs_release_path(path);
  5823. return 0;
  5824. }
  5825. static int log_delayed_insertion_items(struct btrfs_trans_handle *trans,
  5826. struct btrfs_inode *inode,
  5827. struct btrfs_path *path,
  5828. const struct list_head *delayed_ins_list,
  5829. struct btrfs_log_ctx *ctx)
  5830. {
  5831. /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */
  5832. const int max_batch_size = 195;
  5833. const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info);
  5834. const u64 ino = btrfs_ino(inode);
  5835. struct btrfs_root *log = inode->root->log_root;
  5836. struct btrfs_item_batch batch = {
  5837. .nr = 0,
  5838. .total_data_size = 0,
  5839. };
  5840. const struct btrfs_delayed_item *first = NULL;
  5841. const struct btrfs_delayed_item *curr;
  5842. char *ins_data;
  5843. struct btrfs_key *ins_keys;
  5844. u32 *ins_sizes;
  5845. u64 curr_batch_size = 0;
  5846. int batch_idx = 0;
  5847. int ret;
  5848. /* We are adding dir index items to the log tree. */
  5849. lockdep_assert_held(&inode->log_mutex);
  5850. /*
  5851. * We collect delayed items before copying index keys from the subvolume
  5852. * to the log tree. However just after we collected them, they may have
  5853. * been flushed (all of them or just some of them), and therefore we
  5854. * could have copied them from the subvolume tree to the log tree.
  5855. * So find the first delayed item that was not yet logged (they are
  5856. * sorted by index number).
  5857. */
  5858. list_for_each_entry(curr, delayed_ins_list, log_list) {
  5859. if (curr->index > inode->last_dir_index_offset) {
  5860. first = curr;
  5861. break;
  5862. }
  5863. }
  5864. /* Empty list or all delayed items were already logged. */
  5865. if (!first)
  5866. return 0;
  5867. ins_data = kmalloc_array(max_batch_size, sizeof(u32) + sizeof(struct btrfs_key), GFP_NOFS);
  5868. if (!ins_data)
  5869. return -ENOMEM;
  5870. ins_sizes = (u32 *)ins_data;
  5871. batch.data_sizes = ins_sizes;
  5872. ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32));
  5873. batch.keys = ins_keys;
  5874. curr = first;
  5875. while (!list_entry_is_head(curr, delayed_ins_list, log_list)) {
  5876. const u32 curr_size = curr->data_len + sizeof(struct btrfs_item);
  5877. if (curr_batch_size + curr_size > leaf_data_size ||
  5878. batch.nr == max_batch_size) {
  5879. ret = insert_delayed_items_batch(trans, log, path,
  5880. &batch, first);
  5881. if (ret)
  5882. goto out;
  5883. batch_idx = 0;
  5884. batch.nr = 0;
  5885. batch.total_data_size = 0;
  5886. curr_batch_size = 0;
  5887. first = curr;
  5888. }
  5889. ins_sizes[batch_idx] = curr->data_len;
  5890. ins_keys[batch_idx].objectid = ino;
  5891. ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY;
  5892. ins_keys[batch_idx].offset = curr->index;
  5893. curr_batch_size += curr_size;
  5894. batch.total_data_size += curr->data_len;
  5895. batch.nr++;
  5896. batch_idx++;
  5897. curr = list_next_entry(curr, log_list);
  5898. }
  5899. ASSERT(batch.nr >= 1, "batch.nr=%d", batch.nr);
  5900. ret = insert_delayed_items_batch(trans, log, path, &batch, first);
  5901. curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item,
  5902. log_list);
  5903. inode->last_dir_index_offset = curr->index;
  5904. out:
  5905. kfree(ins_data);
  5906. return ret;
  5907. }
  5908. static int log_delayed_deletions_full(struct btrfs_trans_handle *trans,
  5909. struct btrfs_inode *inode,
  5910. struct btrfs_path *path,
  5911. const struct list_head *delayed_del_list,
  5912. struct btrfs_log_ctx *ctx)
  5913. {
  5914. const u64 ino = btrfs_ino(inode);
  5915. const struct btrfs_delayed_item *curr;
  5916. curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
  5917. log_list);
  5918. while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
  5919. u64 first_dir_index = curr->index;
  5920. u64 last_dir_index;
  5921. const struct btrfs_delayed_item *next;
  5922. int ret;
  5923. /*
  5924. * Find a range of consecutive dir index items to delete. Like
  5925. * this we log a single dir range item spanning several contiguous
  5926. * dir items instead of logging one range item per dir index item.
  5927. */
  5928. next = list_next_entry(curr, log_list);
  5929. while (!list_entry_is_head(next, delayed_del_list, log_list)) {
  5930. if (next->index != curr->index + 1)
  5931. break;
  5932. curr = next;
  5933. next = list_next_entry(next, log_list);
  5934. }
  5935. last_dir_index = curr->index;
  5936. ASSERT(last_dir_index >= first_dir_index,
  5937. "last_dir_index=%llu first_dir_index=%llu",
  5938. last_dir_index, first_dir_index);
  5939. ret = insert_dir_log_key(trans, inode->root->log_root, path,
  5940. ino, first_dir_index, last_dir_index);
  5941. if (ret)
  5942. return ret;
  5943. curr = list_next_entry(curr, log_list);
  5944. }
  5945. return 0;
  5946. }
  5947. static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans,
  5948. struct btrfs_inode *inode,
  5949. struct btrfs_path *path,
  5950. const struct list_head *delayed_del_list,
  5951. const struct btrfs_delayed_item *first,
  5952. const struct btrfs_delayed_item **last_ret)
  5953. {
  5954. const struct btrfs_delayed_item *next;
  5955. struct extent_buffer *leaf = path->nodes[0];
  5956. const int last_slot = btrfs_header_nritems(leaf) - 1;
  5957. int slot = path->slots[0] + 1;
  5958. const u64 ino = btrfs_ino(inode);
  5959. next = list_next_entry(first, log_list);
  5960. while (slot < last_slot &&
  5961. !list_entry_is_head(next, delayed_del_list, log_list)) {
  5962. struct btrfs_key key;
  5963. btrfs_item_key_to_cpu(leaf, &key, slot);
  5964. if (key.objectid != ino ||
  5965. key.type != BTRFS_DIR_INDEX_KEY ||
  5966. key.offset != next->index)
  5967. break;
  5968. slot++;
  5969. *last_ret = next;
  5970. next = list_next_entry(next, log_list);
  5971. }
  5972. return btrfs_del_items(trans, inode->root->log_root, path,
  5973. path->slots[0], slot - path->slots[0]);
  5974. }
  5975. static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans,
  5976. struct btrfs_inode *inode,
  5977. struct btrfs_path *path,
  5978. const struct list_head *delayed_del_list,
  5979. struct btrfs_log_ctx *ctx)
  5980. {
  5981. struct btrfs_root *log = inode->root->log_root;
  5982. const struct btrfs_delayed_item *curr;
  5983. u64 last_range_start = 0;
  5984. u64 last_range_end = 0;
  5985. struct btrfs_key key;
  5986. key.objectid = btrfs_ino(inode);
  5987. key.type = BTRFS_DIR_INDEX_KEY;
  5988. curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
  5989. log_list);
  5990. while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
  5991. const struct btrfs_delayed_item *last = curr;
  5992. u64 first_dir_index = curr->index;
  5993. u64 last_dir_index;
  5994. bool deleted_items = false;
  5995. int ret;
  5996. key.offset = curr->index;
  5997. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  5998. if (ret < 0) {
  5999. return ret;
  6000. } else if (ret == 0) {
  6001. ret = batch_delete_dir_index_items(trans, inode, path,
  6002. delayed_del_list, curr,
  6003. &last);
  6004. if (ret)
  6005. return ret;
  6006. deleted_items = true;
  6007. }
  6008. btrfs_release_path(path);
  6009. /*
  6010. * If we deleted items from the leaf, it means we have a range
  6011. * item logging their range, so no need to add one or update an
  6012. * existing one. Otherwise we have to log a dir range item.
  6013. */
  6014. if (deleted_items)
  6015. goto next_batch;
  6016. last_dir_index = last->index;
  6017. ASSERT(last_dir_index >= first_dir_index,
  6018. "last_dir_index=%llu first_dir_index=%llu",
  6019. last_dir_index, first_dir_index);
  6020. /*
  6021. * If this range starts right after where the previous one ends,
  6022. * then we want to reuse the previous range item and change its
  6023. * end offset to the end of this range. This is just to minimize
  6024. * leaf space usage, by avoiding adding a new range item.
  6025. */
  6026. if (last_range_end != 0 && first_dir_index == last_range_end + 1)
  6027. first_dir_index = last_range_start;
  6028. ret = insert_dir_log_key(trans, log, path, key.objectid,
  6029. first_dir_index, last_dir_index);
  6030. if (ret)
  6031. return ret;
  6032. last_range_start = first_dir_index;
  6033. last_range_end = last_dir_index;
  6034. next_batch:
  6035. curr = list_next_entry(last, log_list);
  6036. }
  6037. return 0;
  6038. }
  6039. static int log_delayed_deletion_items(struct btrfs_trans_handle *trans,
  6040. struct btrfs_inode *inode,
  6041. struct btrfs_path *path,
  6042. const struct list_head *delayed_del_list,
  6043. struct btrfs_log_ctx *ctx)
  6044. {
  6045. /*
  6046. * We are deleting dir index items from the log tree or adding range
  6047. * items to it.
  6048. */
  6049. lockdep_assert_held(&inode->log_mutex);
  6050. if (list_empty(delayed_del_list))
  6051. return 0;
  6052. if (ctx->logged_before)
  6053. return log_delayed_deletions_incremental(trans, inode, path,
  6054. delayed_del_list, ctx);
  6055. return log_delayed_deletions_full(trans, inode, path, delayed_del_list,
  6056. ctx);
  6057. }
  6058. /*
  6059. * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed
  6060. * items instead of the subvolume tree.
  6061. */
  6062. static int log_new_delayed_dentries(struct btrfs_trans_handle *trans,
  6063. struct btrfs_inode *inode,
  6064. const struct list_head *delayed_ins_list,
  6065. struct btrfs_log_ctx *ctx)
  6066. {
  6067. const bool orig_log_new_dentries = ctx->log_new_dentries;
  6068. struct btrfs_delayed_item *item;
  6069. int ret = 0;
  6070. /*
  6071. * No need for the log mutex, plus to avoid potential deadlocks or
  6072. * lockdep annotations due to nesting of delayed inode mutexes and log
  6073. * mutexes.
  6074. */
  6075. lockdep_assert_not_held(&inode->log_mutex);
  6076. ASSERT(!ctx->logging_new_delayed_dentries,
  6077. "ctx->logging_new_delayed_dentries=%d", ctx->logging_new_delayed_dentries);
  6078. ctx->logging_new_delayed_dentries = true;
  6079. list_for_each_entry(item, delayed_ins_list, log_list) {
  6080. struct btrfs_dir_item *dir_item;
  6081. struct btrfs_inode *di_inode;
  6082. struct btrfs_key key;
  6083. int log_mode = LOG_INODE_EXISTS;
  6084. dir_item = (struct btrfs_dir_item *)item->data;
  6085. btrfs_disk_key_to_cpu(&key, &dir_item->location);
  6086. if (key.type == BTRFS_ROOT_ITEM_KEY)
  6087. continue;
  6088. di_inode = btrfs_iget_logging(key.objectid, inode->root);
  6089. if (IS_ERR(di_inode)) {
  6090. ret = PTR_ERR(di_inode);
  6091. break;
  6092. }
  6093. if (!need_log_inode(trans, di_inode)) {
  6094. btrfs_add_delayed_iput(di_inode);
  6095. continue;
  6096. }
  6097. if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR)
  6098. log_mode = LOG_INODE_ALL;
  6099. ctx->log_new_dentries = false;
  6100. ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
  6101. if (!ret && ctx->log_new_dentries)
  6102. ret = log_new_dir_dentries(trans, di_inode, ctx);
  6103. btrfs_add_delayed_iput(di_inode);
  6104. if (ret)
  6105. break;
  6106. }
  6107. ctx->log_new_dentries = orig_log_new_dentries;
  6108. ctx->logging_new_delayed_dentries = false;
  6109. return ret;
  6110. }
  6111. /* log a single inode in the tree log.
  6112. * At least one parent directory for this inode must exist in the tree
  6113. * or be logged already.
  6114. *
  6115. * Any items from this inode changed by the current transaction are copied
  6116. * to the log tree. An extra reference is taken on any extents in this
  6117. * file, allowing us to avoid a whole pile of corner cases around logging
  6118. * blocks that have been removed from the tree.
  6119. *
  6120. * See LOG_INODE_ALL and related defines for a description of what inode_only
  6121. * does.
  6122. *
  6123. * This handles both files and directories.
  6124. */
  6125. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  6126. struct btrfs_inode *inode,
  6127. int inode_only,
  6128. struct btrfs_log_ctx *ctx)
  6129. {
  6130. struct btrfs_path *path;
  6131. struct btrfs_path *dst_path;
  6132. struct btrfs_key min_key;
  6133. struct btrfs_key max_key;
  6134. struct btrfs_root *log = inode->root->log_root;
  6135. int ret;
  6136. bool fast_search = false;
  6137. u64 ino = btrfs_ino(inode);
  6138. struct extent_map_tree *em_tree = &inode->extent_tree;
  6139. u64 logged_isize = 0;
  6140. bool need_log_inode_item = true;
  6141. bool xattrs_logged = false;
  6142. bool inode_item_dropped = true;
  6143. bool full_dir_logging = false;
  6144. LIST_HEAD(delayed_ins_list);
  6145. LIST_HEAD(delayed_del_list);
  6146. path = btrfs_alloc_path();
  6147. if (!path)
  6148. return -ENOMEM;
  6149. dst_path = btrfs_alloc_path();
  6150. if (!dst_path) {
  6151. btrfs_free_path(path);
  6152. return -ENOMEM;
  6153. }
  6154. min_key.objectid = ino;
  6155. min_key.type = BTRFS_INODE_ITEM_KEY;
  6156. min_key.offset = 0;
  6157. max_key.objectid = ino;
  6158. /* today the code can only do partial logging of directories */
  6159. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  6160. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  6161. &inode->runtime_flags) &&
  6162. inode_only >= LOG_INODE_EXISTS))
  6163. max_key.type = BTRFS_XATTR_ITEM_KEY;
  6164. else
  6165. max_key.type = (u8)-1;
  6166. max_key.offset = (u64)-1;
  6167. if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL)
  6168. full_dir_logging = true;
  6169. /*
  6170. * If we are logging a directory while we are logging dentries of the
  6171. * delayed items of some other inode, then we need to flush the delayed
  6172. * items of this directory and not log the delayed items directly. This
  6173. * is to prevent more than one level of recursion into btrfs_log_inode()
  6174. * by having something like this:
  6175. *
  6176. * $ mkdir -p a/b/c/d/e/f/g/h/...
  6177. * $ xfs_io -c "fsync" a
  6178. *
  6179. * Where all directories in the path did not exist before and are
  6180. * created in the current transaction.
  6181. * So in such a case we directly log the delayed items of the main
  6182. * directory ("a") without flushing them first, while for each of its
  6183. * subdirectories we flush their delayed items before logging them.
  6184. * This prevents a potential unbounded recursion like this:
  6185. *
  6186. * btrfs_log_inode()
  6187. * log_new_delayed_dentries()
  6188. * btrfs_log_inode()
  6189. * log_new_delayed_dentries()
  6190. * btrfs_log_inode()
  6191. * log_new_delayed_dentries()
  6192. * (...)
  6193. *
  6194. * We have thresholds for the maximum number of delayed items to have in
  6195. * memory, and once they are hit, the items are flushed asynchronously.
  6196. * However the limit is quite high, so lets prevent deep levels of
  6197. * recursion to happen by limiting the maximum depth to be 1.
  6198. */
  6199. if (full_dir_logging && ctx->logging_new_delayed_dentries) {
  6200. ret = btrfs_commit_inode_delayed_items(trans, inode);
  6201. if (ret)
  6202. goto out;
  6203. }
  6204. mutex_lock(&inode->log_mutex);
  6205. /*
  6206. * For symlinks, we must always log their content, which is stored in an
  6207. * inline extent, otherwise we could end up with an empty symlink after
  6208. * log replay, which is invalid on linux (symlink(2) returns -ENOENT if
  6209. * one attempts to create an empty symlink).
  6210. * We don't need to worry about flushing delalloc, because when we create
  6211. * the inline extent when the symlink is created (we never have delalloc
  6212. * for symlinks).
  6213. */
  6214. if (S_ISLNK(inode->vfs_inode.i_mode))
  6215. inode_only = LOG_INODE_ALL;
  6216. /*
  6217. * Before logging the inode item, cache the value returned by
  6218. * inode_logged(), because after that we have the need to figure out if
  6219. * the inode was previously logged in this transaction.
  6220. */
  6221. ret = inode_logged(trans, inode, path);
  6222. if (ret < 0)
  6223. goto out_unlock;
  6224. ctx->logged_before = (ret == 1);
  6225. ret = 0;
  6226. /*
  6227. * This is for cases where logging a directory could result in losing a
  6228. * a file after replaying the log. For example, if we move a file from a
  6229. * directory A to a directory B, then fsync directory A, we have no way
  6230. * to known the file was moved from A to B, so logging just A would
  6231. * result in losing the file after a log replay.
  6232. */
  6233. if (full_dir_logging && inode->last_unlink_trans >= trans->transid) {
  6234. ret = BTRFS_LOG_FORCE_COMMIT;
  6235. goto out_unlock;
  6236. }
  6237. /*
  6238. * a brute force approach to making sure we get the most uptodate
  6239. * copies of everything.
  6240. */
  6241. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  6242. clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
  6243. if (ctx->logged_before)
  6244. ret = drop_inode_items(trans, log, path, inode,
  6245. BTRFS_XATTR_ITEM_KEY);
  6246. } else {
  6247. if (inode_only == LOG_INODE_EXISTS) {
  6248. /*
  6249. * Make sure the new inode item we write to the log has
  6250. * the same isize as the current one (if it exists).
  6251. * This is necessary to prevent data loss after log
  6252. * replay, and also to prevent doing a wrong expanding
  6253. * truncate - for e.g. create file, write 4K into offset
  6254. * 0, fsync, write 4K into offset 4096, add hard link,
  6255. * fsync some other file (to sync log), power fail - if
  6256. * we use the inode's current i_size, after log replay
  6257. * we get a 8Kb file, with the last 4Kb extent as a hole
  6258. * (zeroes), as if an expanding truncate happened,
  6259. * instead of getting a file of 4Kb only.
  6260. */
  6261. ret = get_inode_size_to_log(trans, inode, path, &logged_isize);
  6262. if (ret)
  6263. goto out_unlock;
  6264. }
  6265. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  6266. &inode->runtime_flags)) {
  6267. if (inode_only == LOG_INODE_EXISTS) {
  6268. max_key.type = BTRFS_XATTR_ITEM_KEY;
  6269. if (ctx->logged_before)
  6270. ret = drop_inode_items(trans, log, path,
  6271. inode, max_key.type);
  6272. } else {
  6273. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  6274. &inode->runtime_flags);
  6275. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  6276. &inode->runtime_flags);
  6277. if (ctx->logged_before)
  6278. ret = truncate_inode_items(trans, log,
  6279. inode, 0, 0);
  6280. }
  6281. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  6282. &inode->runtime_flags) ||
  6283. inode_only == LOG_INODE_EXISTS) {
  6284. if (inode_only == LOG_INODE_ALL)
  6285. fast_search = true;
  6286. max_key.type = BTRFS_XATTR_ITEM_KEY;
  6287. if (ctx->logged_before)
  6288. ret = drop_inode_items(trans, log, path, inode,
  6289. max_key.type);
  6290. } else {
  6291. if (inode_only == LOG_INODE_ALL)
  6292. fast_search = true;
  6293. inode_item_dropped = false;
  6294. goto log_extents;
  6295. }
  6296. }
  6297. if (ret)
  6298. goto out_unlock;
  6299. /*
  6300. * If we are logging a directory in full mode, collect the delayed items
  6301. * before iterating the subvolume tree, so that we don't miss any new
  6302. * dir index items in case they get flushed while or right after we are
  6303. * iterating the subvolume tree.
  6304. */
  6305. if (full_dir_logging && !ctx->logging_new_delayed_dentries)
  6306. btrfs_log_get_delayed_items(inode, &delayed_ins_list,
  6307. &delayed_del_list);
  6308. /*
  6309. * If we are fsyncing a file with 0 hard links, then commit the delayed
  6310. * inode because the last inode ref (or extref) item may still be in the
  6311. * subvolume tree and if we log it the file will still exist after a log
  6312. * replay. So commit the delayed inode to delete that last ref and we
  6313. * skip logging it.
  6314. */
  6315. if (inode->vfs_inode.i_nlink == 0) {
  6316. ret = btrfs_commit_inode_delayed_inode(inode);
  6317. if (ret)
  6318. goto out_unlock;
  6319. }
  6320. ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
  6321. path, dst_path, logged_isize,
  6322. inode_only, ctx,
  6323. &need_log_inode_item);
  6324. if (ret)
  6325. goto out_unlock;
  6326. btrfs_release_path(path);
  6327. btrfs_release_path(dst_path);
  6328. ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
  6329. if (ret)
  6330. goto out_unlock;
  6331. xattrs_logged = true;
  6332. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  6333. btrfs_release_path(path);
  6334. btrfs_release_path(dst_path);
  6335. ret = btrfs_log_holes(trans, inode, path);
  6336. if (ret)
  6337. goto out_unlock;
  6338. }
  6339. log_extents:
  6340. btrfs_release_path(path);
  6341. btrfs_release_path(dst_path);
  6342. if (need_log_inode_item) {
  6343. ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
  6344. if (ret)
  6345. goto out_unlock;
  6346. /*
  6347. * If we are doing a fast fsync and the inode was logged before
  6348. * in this transaction, we don't need to log the xattrs because
  6349. * they were logged before. If xattrs were added, changed or
  6350. * deleted since the last time we logged the inode, then we have
  6351. * already logged them because the inode had the runtime flag
  6352. * BTRFS_INODE_COPY_EVERYTHING set.
  6353. */
  6354. if (!xattrs_logged && inode->logged_trans < trans->transid) {
  6355. ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
  6356. if (ret)
  6357. goto out_unlock;
  6358. btrfs_release_path(path);
  6359. }
  6360. }
  6361. if (fast_search) {
  6362. ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
  6363. if (ret)
  6364. goto out_unlock;
  6365. } else if (inode_only == LOG_INODE_ALL) {
  6366. struct extent_map *em, *n;
  6367. write_lock(&em_tree->lock);
  6368. list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
  6369. list_del_init(&em->list);
  6370. write_unlock(&em_tree->lock);
  6371. }
  6372. if (full_dir_logging) {
  6373. ret = log_directory_changes(trans, inode, path, dst_path, ctx);
  6374. if (ret)
  6375. goto out_unlock;
  6376. ret = log_delayed_insertion_items(trans, inode, path,
  6377. &delayed_ins_list, ctx);
  6378. if (ret)
  6379. goto out_unlock;
  6380. ret = log_delayed_deletion_items(trans, inode, path,
  6381. &delayed_del_list, ctx);
  6382. if (ret)
  6383. goto out_unlock;
  6384. }
  6385. spin_lock(&inode->lock);
  6386. inode->logged_trans = trans->transid;
  6387. /*
  6388. * Don't update last_log_commit if we logged that an inode exists.
  6389. * We do this for three reasons:
  6390. *
  6391. * 1) We might have had buffered writes to this inode that were
  6392. * flushed and had their ordered extents completed in this
  6393. * transaction, but we did not previously log the inode with
  6394. * LOG_INODE_ALL. Later the inode was evicted and after that
  6395. * it was loaded again and this LOG_INODE_EXISTS log operation
  6396. * happened. We must make sure that if an explicit fsync against
  6397. * the inode is performed later, it logs the new extents, an
  6398. * updated inode item, etc, and syncs the log. The same logic
  6399. * applies to direct IO writes instead of buffered writes.
  6400. *
  6401. * 2) When we log the inode with LOG_INODE_EXISTS, its inode item
  6402. * is logged with an i_size of 0 or whatever value was logged
  6403. * before. If later the i_size of the inode is increased by a
  6404. * truncate operation, the log is synced through an fsync of
  6405. * some other inode and then finally an explicit fsync against
  6406. * this inode is made, we must make sure this fsync logs the
  6407. * inode with the new i_size, the hole between old i_size and
  6408. * the new i_size, and syncs the log.
  6409. *
  6410. * 3) If we are logging that an ancestor inode exists as part of
  6411. * logging a new name from a link or rename operation, don't update
  6412. * its last_log_commit - otherwise if an explicit fsync is made
  6413. * against an ancestor, the fsync considers the inode in the log
  6414. * and doesn't sync the log, resulting in the ancestor missing after
  6415. * a power failure unless the log was synced as part of an fsync
  6416. * against any other unrelated inode.
  6417. */
  6418. if (!ctx->logging_new_name && inode_only != LOG_INODE_EXISTS)
  6419. inode->last_log_commit = inode->last_sub_trans;
  6420. spin_unlock(&inode->lock);
  6421. /*
  6422. * Reset the last_reflink_trans so that the next fsync does not need to
  6423. * go through the slower path when logging extents and their checksums.
  6424. */
  6425. if (inode_only == LOG_INODE_ALL)
  6426. inode->last_reflink_trans = 0;
  6427. out_unlock:
  6428. mutex_unlock(&inode->log_mutex);
  6429. out:
  6430. btrfs_free_path(path);
  6431. btrfs_free_path(dst_path);
  6432. if (ret)
  6433. free_conflicting_inodes(ctx);
  6434. else
  6435. ret = log_conflicting_inodes(trans, inode->root, ctx);
  6436. if (full_dir_logging && !ctx->logging_new_delayed_dentries) {
  6437. if (!ret)
  6438. ret = log_new_delayed_dentries(trans, inode,
  6439. &delayed_ins_list, ctx);
  6440. btrfs_log_put_delayed_items(inode, &delayed_ins_list,
  6441. &delayed_del_list);
  6442. }
  6443. return ret;
  6444. }
  6445. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  6446. struct btrfs_inode *inode,
  6447. struct btrfs_log_ctx *ctx)
  6448. {
  6449. int ret;
  6450. BTRFS_PATH_AUTO_FREE(path);
  6451. struct btrfs_key key;
  6452. struct btrfs_root *root = inode->root;
  6453. const u64 ino = btrfs_ino(inode);
  6454. path = btrfs_alloc_path();
  6455. if (!path)
  6456. return -ENOMEM;
  6457. path->skip_locking = true;
  6458. path->search_commit_root = true;
  6459. key.objectid = ino;
  6460. key.type = BTRFS_INODE_REF_KEY;
  6461. key.offset = 0;
  6462. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6463. if (ret < 0)
  6464. return ret;
  6465. while (true) {
  6466. struct extent_buffer *leaf = path->nodes[0];
  6467. int slot = path->slots[0];
  6468. u32 cur_offset = 0;
  6469. u32 item_size;
  6470. unsigned long ptr;
  6471. if (slot >= btrfs_header_nritems(leaf)) {
  6472. ret = btrfs_next_leaf(root, path);
  6473. if (ret < 0)
  6474. return ret;
  6475. if (ret > 0)
  6476. break;
  6477. continue;
  6478. }
  6479. btrfs_item_key_to_cpu(leaf, &key, slot);
  6480. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  6481. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  6482. break;
  6483. item_size = btrfs_item_size(leaf, slot);
  6484. ptr = btrfs_item_ptr_offset(leaf, slot);
  6485. while (cur_offset < item_size) {
  6486. u64 dir_id;
  6487. struct btrfs_inode *dir_inode;
  6488. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  6489. struct btrfs_inode_extref *extref;
  6490. extref = (struct btrfs_inode_extref *)
  6491. (ptr + cur_offset);
  6492. dir_id = btrfs_inode_extref_parent(leaf, extref);
  6493. cur_offset += sizeof(*extref);
  6494. cur_offset += btrfs_inode_extref_name_len(leaf,
  6495. extref);
  6496. } else {
  6497. dir_id = key.offset;
  6498. cur_offset = item_size;
  6499. }
  6500. dir_inode = btrfs_iget_logging(dir_id, root);
  6501. /*
  6502. * If the parent inode was deleted, return an error to
  6503. * fallback to a transaction commit. This is to prevent
  6504. * getting an inode that was moved from one parent A to
  6505. * a parent B, got its former parent A deleted and then
  6506. * it got fsync'ed, from existing at both parents after
  6507. * a log replay (and the old parent still existing).
  6508. * Example:
  6509. *
  6510. * mkdir /mnt/A
  6511. * mkdir /mnt/B
  6512. * touch /mnt/B/bar
  6513. * sync
  6514. * mv /mnt/B/bar /mnt/A/bar
  6515. * mv -T /mnt/A /mnt/B
  6516. * fsync /mnt/B/bar
  6517. * <power fail>
  6518. *
  6519. * If we ignore the old parent B which got deleted,
  6520. * after a log replay we would have file bar linked
  6521. * at both parents and the old parent B would still
  6522. * exist.
  6523. */
  6524. if (IS_ERR(dir_inode))
  6525. return PTR_ERR(dir_inode);
  6526. if (!need_log_inode(trans, dir_inode)) {
  6527. btrfs_add_delayed_iput(dir_inode);
  6528. continue;
  6529. }
  6530. ctx->log_new_dentries = false;
  6531. ret = btrfs_log_inode(trans, dir_inode, LOG_INODE_ALL, ctx);
  6532. if (!ret && ctx->log_new_dentries)
  6533. ret = log_new_dir_dentries(trans, dir_inode, ctx);
  6534. btrfs_add_delayed_iput(dir_inode);
  6535. if (ret)
  6536. return ret;
  6537. }
  6538. path->slots[0]++;
  6539. }
  6540. return 0;
  6541. }
  6542. static int log_new_ancestors(struct btrfs_trans_handle *trans,
  6543. struct btrfs_root *root,
  6544. struct btrfs_path *path,
  6545. struct btrfs_log_ctx *ctx)
  6546. {
  6547. struct btrfs_key found_key;
  6548. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  6549. while (true) {
  6550. struct extent_buffer *leaf;
  6551. int slot;
  6552. struct btrfs_key search_key;
  6553. struct btrfs_inode *inode;
  6554. u64 ino;
  6555. int ret = 0;
  6556. btrfs_release_path(path);
  6557. ino = found_key.offset;
  6558. search_key.objectid = found_key.offset;
  6559. search_key.type = BTRFS_INODE_ITEM_KEY;
  6560. search_key.offset = 0;
  6561. inode = btrfs_iget_logging(ino, root);
  6562. if (IS_ERR(inode))
  6563. return PTR_ERR(inode);
  6564. if (inode->generation >= trans->transid &&
  6565. need_log_inode(trans, inode))
  6566. ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
  6567. btrfs_add_delayed_iput(inode);
  6568. if (ret)
  6569. return ret;
  6570. if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
  6571. break;
  6572. search_key.type = BTRFS_INODE_REF_KEY;
  6573. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  6574. if (ret < 0)
  6575. return ret;
  6576. leaf = path->nodes[0];
  6577. slot = path->slots[0];
  6578. if (slot >= btrfs_header_nritems(leaf)) {
  6579. ret = btrfs_next_leaf(root, path);
  6580. if (ret < 0)
  6581. return ret;
  6582. else if (ret > 0)
  6583. return -ENOENT;
  6584. leaf = path->nodes[0];
  6585. slot = path->slots[0];
  6586. }
  6587. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6588. if (found_key.objectid != search_key.objectid ||
  6589. found_key.type != BTRFS_INODE_REF_KEY)
  6590. return -ENOENT;
  6591. }
  6592. return 0;
  6593. }
  6594. static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
  6595. struct btrfs_inode *inode,
  6596. struct dentry *parent,
  6597. struct btrfs_log_ctx *ctx)
  6598. {
  6599. struct btrfs_root *root = inode->root;
  6600. struct dentry *old_parent = NULL;
  6601. struct super_block *sb = inode->vfs_inode.i_sb;
  6602. int ret = 0;
  6603. while (true) {
  6604. if (!parent || d_really_is_negative(parent) ||
  6605. sb != parent->d_sb)
  6606. break;
  6607. inode = BTRFS_I(d_inode(parent));
  6608. if (root != inode->root)
  6609. break;
  6610. if (inode->generation >= trans->transid &&
  6611. need_log_inode(trans, inode)) {
  6612. ret = btrfs_log_inode(trans, inode,
  6613. LOG_INODE_EXISTS, ctx);
  6614. if (ret)
  6615. break;
  6616. }
  6617. if (IS_ROOT(parent))
  6618. break;
  6619. parent = dget_parent(parent);
  6620. dput(old_parent);
  6621. old_parent = parent;
  6622. }
  6623. dput(old_parent);
  6624. return ret;
  6625. }
  6626. static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
  6627. struct btrfs_inode *inode,
  6628. struct dentry *parent,
  6629. struct btrfs_log_ctx *ctx)
  6630. {
  6631. struct btrfs_root *root = inode->root;
  6632. const u64 ino = btrfs_ino(inode);
  6633. BTRFS_PATH_AUTO_FREE(path);
  6634. struct btrfs_key search_key;
  6635. int ret;
  6636. /*
  6637. * For a single hard link case, go through a fast path that does not
  6638. * need to iterate the fs/subvolume tree.
  6639. */
  6640. if (inode->vfs_inode.i_nlink < 2)
  6641. return log_new_ancestors_fast(trans, inode, parent, ctx);
  6642. path = btrfs_alloc_path();
  6643. if (!path)
  6644. return -ENOMEM;
  6645. search_key.objectid = ino;
  6646. search_key.type = BTRFS_INODE_REF_KEY;
  6647. search_key.offset = 0;
  6648. again:
  6649. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  6650. if (ret < 0)
  6651. return ret;
  6652. if (ret == 0)
  6653. path->slots[0]++;
  6654. while (true) {
  6655. struct extent_buffer *leaf = path->nodes[0];
  6656. int slot = path->slots[0];
  6657. struct btrfs_key found_key;
  6658. if (slot >= btrfs_header_nritems(leaf)) {
  6659. ret = btrfs_next_leaf(root, path);
  6660. if (ret < 0)
  6661. return ret;
  6662. if (ret > 0)
  6663. break;
  6664. continue;
  6665. }
  6666. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6667. if (found_key.objectid != ino ||
  6668. found_key.type > BTRFS_INODE_EXTREF_KEY)
  6669. break;
  6670. /*
  6671. * Don't deal with extended references because they are rare
  6672. * cases and too complex to deal with (we would need to keep
  6673. * track of which subitem we are processing for each item in
  6674. * this loop, etc). So just return some error to fallback to
  6675. * a transaction commit.
  6676. */
  6677. if (found_key.type == BTRFS_INODE_EXTREF_KEY)
  6678. return -EMLINK;
  6679. /*
  6680. * Logging ancestors needs to do more searches on the fs/subvol
  6681. * tree, so it releases the path as needed to avoid deadlocks.
  6682. * Keep track of the last inode ref key and resume from that key
  6683. * after logging all new ancestors for the current hard link.
  6684. */
  6685. memcpy(&search_key, &found_key, sizeof(search_key));
  6686. ret = log_new_ancestors(trans, root, path, ctx);
  6687. if (ret)
  6688. return ret;
  6689. btrfs_release_path(path);
  6690. goto again;
  6691. }
  6692. return 0;
  6693. }
  6694. /*
  6695. * helper function around btrfs_log_inode to make sure newly created
  6696. * parent directories also end up in the log. A minimal inode and backref
  6697. * only logging is done of any parent directories that are older than
  6698. * the last committed transaction
  6699. */
  6700. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  6701. struct btrfs_inode *inode,
  6702. struct dentry *parent,
  6703. int inode_only,
  6704. struct btrfs_log_ctx *ctx)
  6705. {
  6706. struct btrfs_root *root = inode->root;
  6707. struct btrfs_fs_info *fs_info = root->fs_info;
  6708. int ret = 0;
  6709. bool log_dentries;
  6710. if (btrfs_test_opt(fs_info, NOTREELOG))
  6711. return BTRFS_LOG_FORCE_COMMIT;
  6712. if (btrfs_root_refs(&root->root_item) == 0)
  6713. return BTRFS_LOG_FORCE_COMMIT;
  6714. /*
  6715. * If we're logging an inode from a subvolume created in the current
  6716. * transaction we must force a commit since the root is not persisted.
  6717. */
  6718. if (btrfs_root_generation(&root->root_item) == trans->transid)
  6719. return BTRFS_LOG_FORCE_COMMIT;
  6720. /* Skip already logged inodes and without new extents. */
  6721. if (btrfs_inode_in_log(inode, trans->transid) &&
  6722. list_empty(&ctx->ordered_extents))
  6723. return BTRFS_NO_LOG_SYNC;
  6724. ret = start_log_trans(trans, root, ctx);
  6725. if (ret)
  6726. return ret;
  6727. ret = btrfs_log_inode(trans, inode, inode_only, ctx);
  6728. if (ret)
  6729. goto end_trans;
  6730. /*
  6731. * for regular files, if its inode is already on disk, we don't
  6732. * have to worry about the parents at all. This is because
  6733. * we can use the last_unlink_trans field to record renames
  6734. * and other fun in this file.
  6735. */
  6736. if (S_ISREG(inode->vfs_inode.i_mode) &&
  6737. inode->generation < trans->transid &&
  6738. inode->last_unlink_trans < trans->transid) {
  6739. ret = 0;
  6740. goto end_trans;
  6741. }
  6742. /*
  6743. * Track if we need to log dentries because ctx->log_new_dentries can
  6744. * be modified in the call chains below.
  6745. */
  6746. log_dentries = ctx->log_new_dentries;
  6747. /*
  6748. * On unlink we must make sure all our current and old parent directory
  6749. * inodes are fully logged. This is to prevent leaving dangling
  6750. * directory index entries in directories that were our parents but are
  6751. * not anymore. Not doing this results in old parent directory being
  6752. * impossible to delete after log replay (rmdir will always fail with
  6753. * error -ENOTEMPTY).
  6754. *
  6755. * Example 1:
  6756. *
  6757. * mkdir testdir
  6758. * touch testdir/foo
  6759. * ln testdir/foo testdir/bar
  6760. * sync
  6761. * unlink testdir/bar
  6762. * xfs_io -c fsync testdir/foo
  6763. * <power failure>
  6764. * mount fs, triggers log replay
  6765. *
  6766. * If we don't log the parent directory (testdir), after log replay the
  6767. * directory still has an entry pointing to the file inode using the bar
  6768. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  6769. * the file inode has a link count of 1.
  6770. *
  6771. * Example 2:
  6772. *
  6773. * mkdir testdir
  6774. * touch foo
  6775. * ln foo testdir/foo2
  6776. * ln foo testdir/foo3
  6777. * sync
  6778. * unlink testdir/foo3
  6779. * xfs_io -c fsync foo
  6780. * <power failure>
  6781. * mount fs, triggers log replay
  6782. *
  6783. * Similar as the first example, after log replay the parent directory
  6784. * testdir still has an entry pointing to the inode file with name foo3
  6785. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  6786. * and has a link count of 2.
  6787. */
  6788. if (inode->last_unlink_trans >= trans->transid) {
  6789. ret = btrfs_log_all_parents(trans, inode, ctx);
  6790. if (ret)
  6791. goto end_trans;
  6792. }
  6793. ret = log_all_new_ancestors(trans, inode, parent, ctx);
  6794. if (ret)
  6795. goto end_trans;
  6796. if (log_dentries)
  6797. ret = log_new_dir_dentries(trans, inode, ctx);
  6798. end_trans:
  6799. if (ret < 0) {
  6800. btrfs_set_log_full_commit(trans);
  6801. ret = BTRFS_LOG_FORCE_COMMIT;
  6802. }
  6803. if (ret)
  6804. btrfs_remove_log_ctx(root, ctx);
  6805. btrfs_end_log_trans(root);
  6806. return ret;
  6807. }
  6808. /*
  6809. * it is not safe to log dentry if the chunk root has added new
  6810. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  6811. * If this returns 1, you must commit the transaction to safely get your
  6812. * data on disk.
  6813. */
  6814. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  6815. struct dentry *dentry,
  6816. struct btrfs_log_ctx *ctx)
  6817. {
  6818. struct dentry *parent = dget_parent(dentry);
  6819. int ret;
  6820. ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
  6821. LOG_INODE_ALL, ctx);
  6822. dput(parent);
  6823. return ret;
  6824. }
  6825. /*
  6826. * should be called during mount to recover any replay any log trees
  6827. * from the FS
  6828. */
  6829. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  6830. {
  6831. int ret;
  6832. struct btrfs_path *path;
  6833. struct btrfs_trans_handle *trans;
  6834. struct btrfs_key key;
  6835. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  6836. struct walk_control wc = {
  6837. .process_func = process_one_buffer,
  6838. .stage = LOG_WALK_PIN_ONLY,
  6839. };
  6840. path = btrfs_alloc_path();
  6841. if (!path)
  6842. return -ENOMEM;
  6843. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  6844. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  6845. if (IS_ERR(trans)) {
  6846. ret = PTR_ERR(trans);
  6847. goto error;
  6848. }
  6849. wc.trans = trans;
  6850. wc.pin = true;
  6851. wc.log = log_root_tree;
  6852. ret = walk_log_tree(&wc);
  6853. wc.log = NULL;
  6854. if (unlikely(ret)) {
  6855. btrfs_abort_transaction(trans, ret);
  6856. goto error;
  6857. }
  6858. again:
  6859. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  6860. key.type = BTRFS_ROOT_ITEM_KEY;
  6861. key.offset = (u64)-1;
  6862. while (1) {
  6863. struct btrfs_key found_key;
  6864. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  6865. if (unlikely(ret < 0)) {
  6866. btrfs_abort_transaction(trans, ret);
  6867. goto error;
  6868. }
  6869. if (ret > 0) {
  6870. if (path->slots[0] == 0)
  6871. break;
  6872. path->slots[0]--;
  6873. }
  6874. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  6875. path->slots[0]);
  6876. btrfs_release_path(path);
  6877. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  6878. break;
  6879. wc.log = btrfs_read_tree_root(log_root_tree, &found_key);
  6880. if (IS_ERR(wc.log)) {
  6881. ret = PTR_ERR(wc.log);
  6882. wc.log = NULL;
  6883. btrfs_abort_transaction(trans, ret);
  6884. goto error;
  6885. }
  6886. wc.root = btrfs_get_fs_root(fs_info, found_key.offset, true);
  6887. if (IS_ERR(wc.root)) {
  6888. ret = PTR_ERR(wc.root);
  6889. wc.root = NULL;
  6890. if (unlikely(ret != -ENOENT)) {
  6891. btrfs_abort_transaction(trans, ret);
  6892. goto error;
  6893. }
  6894. /*
  6895. * We didn't find the subvol, likely because it was
  6896. * deleted. This is ok, simply skip this log and go to
  6897. * the next one.
  6898. *
  6899. * We need to exclude the root because we can't have
  6900. * other log replays overwriting this log as we'll read
  6901. * it back in a few more times. This will keep our
  6902. * block from being modified, and we'll just bail for
  6903. * each subsequent pass.
  6904. */
  6905. ret = btrfs_pin_extent_for_log_replay(trans, wc.log->node);
  6906. if (unlikely(ret)) {
  6907. btrfs_abort_transaction(trans, ret);
  6908. goto error;
  6909. }
  6910. goto next;
  6911. }
  6912. wc.root->log_root = wc.log;
  6913. ret = btrfs_record_root_in_trans(trans, wc.root);
  6914. if (unlikely(ret)) {
  6915. btrfs_abort_transaction(trans, ret);
  6916. goto next;
  6917. }
  6918. ret = walk_log_tree(&wc);
  6919. if (unlikely(ret)) {
  6920. btrfs_abort_transaction(trans, ret);
  6921. goto next;
  6922. }
  6923. if (wc.stage == LOG_WALK_REPLAY_ALL) {
  6924. struct btrfs_root *root = wc.root;
  6925. wc.subvol_path = path;
  6926. ret = fixup_inode_link_counts(&wc);
  6927. wc.subvol_path = NULL;
  6928. if (unlikely(ret)) {
  6929. btrfs_abort_transaction(trans, ret);
  6930. goto next;
  6931. }
  6932. /*
  6933. * We have just replayed everything, and the highest
  6934. * objectid of fs roots probably has changed in case
  6935. * some inode_item's got replayed.
  6936. *
  6937. * root->objectid_mutex is not acquired as log replay
  6938. * could only happen during mount.
  6939. */
  6940. ret = btrfs_init_root_free_objectid(root);
  6941. if (unlikely(ret)) {
  6942. btrfs_abort_transaction(trans, ret);
  6943. goto next;
  6944. }
  6945. }
  6946. next:
  6947. if (wc.root) {
  6948. wc.root->log_root = NULL;
  6949. btrfs_put_root(wc.root);
  6950. }
  6951. btrfs_put_root(wc.log);
  6952. wc.log = NULL;
  6953. if (ret)
  6954. goto error;
  6955. if (found_key.offset == 0)
  6956. break;
  6957. key.offset = found_key.offset - 1;
  6958. }
  6959. btrfs_release_path(path);
  6960. /* step one is to pin it all, step two is to replay just inodes */
  6961. if (wc.pin) {
  6962. wc.pin = false;
  6963. wc.process_func = replay_one_buffer;
  6964. wc.stage = LOG_WALK_REPLAY_INODES;
  6965. goto again;
  6966. }
  6967. /* step three is to replay everything */
  6968. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  6969. wc.stage++;
  6970. goto again;
  6971. }
  6972. btrfs_free_path(path);
  6973. /* step 4: commit the transaction, which also unpins the blocks */
  6974. ret = btrfs_commit_transaction(trans);
  6975. if (ret)
  6976. return ret;
  6977. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  6978. return 0;
  6979. error:
  6980. if (wc.trans)
  6981. btrfs_end_transaction(wc.trans);
  6982. btrfs_put_root(wc.log);
  6983. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  6984. btrfs_free_path(path);
  6985. return ret;
  6986. }
  6987. /*
  6988. * there are some corner cases where we want to force a full
  6989. * commit instead of allowing a directory to be logged.
  6990. *
  6991. * They revolve around files there were unlinked from the directory, and
  6992. * this function updates the parent directory so that a full commit is
  6993. * properly done if it is fsync'd later after the unlinks are done.
  6994. *
  6995. * Must be called before the unlink operations (updates to the subvolume tree,
  6996. * inodes, etc) are done.
  6997. */
  6998. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  6999. struct btrfs_inode *dir, struct btrfs_inode *inode,
  7000. bool for_rename)
  7001. {
  7002. /*
  7003. * when we're logging a file, if it hasn't been renamed
  7004. * or unlinked, and its inode is fully committed on disk,
  7005. * we don't have to worry about walking up the directory chain
  7006. * to log its parents.
  7007. *
  7008. * So, we use the last_unlink_trans field to put this transid
  7009. * into the file. When the file is logged we check it and
  7010. * don't log the parents if the file is fully on disk.
  7011. */
  7012. mutex_lock(&inode->log_mutex);
  7013. inode->last_unlink_trans = trans->transid;
  7014. mutex_unlock(&inode->log_mutex);
  7015. if (!for_rename)
  7016. return;
  7017. /*
  7018. * If this directory was already logged, any new names will be logged
  7019. * with btrfs_log_new_name() and old names will be deleted from the log
  7020. * tree with btrfs_del_dir_entries_in_log() or with
  7021. * btrfs_del_inode_ref_in_log().
  7022. */
  7023. if (inode_logged(trans, dir, NULL) == 1)
  7024. return;
  7025. /*
  7026. * If the inode we're about to unlink was logged before, the log will be
  7027. * properly updated with the new name with btrfs_log_new_name() and the
  7028. * old name removed with btrfs_del_dir_entries_in_log() or with
  7029. * btrfs_del_inode_ref_in_log().
  7030. */
  7031. if (inode_logged(trans, inode, NULL) == 1)
  7032. return;
  7033. /*
  7034. * when renaming files across directories, if the directory
  7035. * there we're unlinking from gets fsync'd later on, there's
  7036. * no way to find the destination directory later and fsync it
  7037. * properly. So, we have to be conservative and force commits
  7038. * so the new name gets discovered.
  7039. */
  7040. mutex_lock(&dir->log_mutex);
  7041. dir->last_unlink_trans = trans->transid;
  7042. mutex_unlock(&dir->log_mutex);
  7043. }
  7044. /*
  7045. * Make sure that if someone attempts to fsync the parent directory of a deleted
  7046. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  7047. * that after replaying the log tree of the parent directory's root we will not
  7048. * see the snapshot anymore and at log replay time we will not see any log tree
  7049. * corresponding to the deleted snapshot's root, which could lead to replaying
  7050. * it after replaying the log tree of the parent directory (which would replay
  7051. * the snapshot delete operation).
  7052. *
  7053. * Must be called before the actual snapshot destroy operation (updates to the
  7054. * parent root and tree of tree roots trees, etc) are done.
  7055. */
  7056. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  7057. struct btrfs_inode *dir)
  7058. {
  7059. mutex_lock(&dir->log_mutex);
  7060. dir->last_unlink_trans = trans->transid;
  7061. mutex_unlock(&dir->log_mutex);
  7062. }
  7063. /*
  7064. * Call this when creating a subvolume in a directory.
  7065. * Because we don't commit a transaction when creating a subvolume, we can't
  7066. * allow the directory pointing to the subvolume to be logged with an entry that
  7067. * points to an unpersisted root if we are still in the transaction used to
  7068. * create the subvolume, so make any attempt to log the directory to result in a
  7069. * full log sync.
  7070. * Also we don't need to worry with renames, since btrfs_rename() marks the log
  7071. * for full commit when renaming a subvolume.
  7072. *
  7073. * Must be called before creating the subvolume entry in its parent directory.
  7074. */
  7075. void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans,
  7076. struct btrfs_inode *dir)
  7077. {
  7078. mutex_lock(&dir->log_mutex);
  7079. dir->last_unlink_trans = trans->transid;
  7080. mutex_unlock(&dir->log_mutex);
  7081. }
  7082. /*
  7083. * Update the log after adding a new name for an inode.
  7084. *
  7085. * @trans: Transaction handle.
  7086. * @old_dentry: The dentry associated with the old name and the old
  7087. * parent directory.
  7088. * @old_dir: The inode of the previous parent directory for the case
  7089. * of a rename. For a link operation, it must be NULL.
  7090. * @old_dir_index: The index number associated with the old name, meaningful
  7091. * only for rename operations (when @old_dir is not NULL).
  7092. * Ignored for link operations.
  7093. * @parent: The dentry associated with the directory under which the
  7094. * new name is located.
  7095. *
  7096. * Call this after adding a new name for an inode, as a result of a link or
  7097. * rename operation, and it will properly update the log to reflect the new name.
  7098. */
  7099. void btrfs_log_new_name(struct btrfs_trans_handle *trans,
  7100. struct dentry *old_dentry, struct btrfs_inode *old_dir,
  7101. u64 old_dir_index, struct dentry *parent)
  7102. {
  7103. struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry));
  7104. struct btrfs_root *root = inode->root;
  7105. struct btrfs_log_ctx ctx;
  7106. bool log_pinned = false;
  7107. int ret;
  7108. /* The inode has a new name (ref/extref), so make sure we log it. */
  7109. set_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
  7110. btrfs_init_log_ctx(&ctx, inode);
  7111. ctx.logging_new_name = true;
  7112. /*
  7113. * this will force the logging code to walk the dentry chain
  7114. * up for the file
  7115. */
  7116. if (!S_ISDIR(inode->vfs_inode.i_mode))
  7117. inode->last_unlink_trans = trans->transid;
  7118. /*
  7119. * if this inode hasn't been logged and directory we're renaming it
  7120. * from hasn't been logged, we don't need to log it
  7121. */
  7122. ret = inode_logged(trans, inode, NULL);
  7123. if (ret < 0) {
  7124. goto out;
  7125. } else if (ret == 0) {
  7126. if (!old_dir)
  7127. return;
  7128. /*
  7129. * If the inode was not logged and we are doing a rename (old_dir is not
  7130. * NULL), check if old_dir was logged - if it was not we can return and
  7131. * do nothing.
  7132. */
  7133. ret = inode_logged(trans, old_dir, NULL);
  7134. if (ret < 0)
  7135. goto out;
  7136. else if (ret == 0)
  7137. return;
  7138. }
  7139. ret = 0;
  7140. /*
  7141. * Now that we know we need to update the log, allocate the scratch eb
  7142. * for the context before joining a log transaction below, as this can
  7143. * take time and therefore we could delay log commits from other tasks.
  7144. */
  7145. btrfs_init_log_ctx_scratch_eb(&ctx);
  7146. /*
  7147. * If we are doing a rename (old_dir is not NULL) from a directory that
  7148. * was previously logged, make sure that on log replay we get the old
  7149. * dir entry deleted. This is needed because we will also log the new
  7150. * name of the renamed inode, so we need to make sure that after log
  7151. * replay we don't end up with both the new and old dir entries existing.
  7152. */
  7153. if (old_dir && old_dir->logged_trans == trans->transid) {
  7154. struct btrfs_root *log = old_dir->root->log_root;
  7155. struct btrfs_path *path;
  7156. struct fscrypt_name fname;
  7157. ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX,
  7158. "old_dir_index=%llu", old_dir_index);
  7159. ret = fscrypt_setup_filename(&old_dir->vfs_inode,
  7160. &old_dentry->d_name, 0, &fname);
  7161. if (ret)
  7162. goto out;
  7163. path = btrfs_alloc_path();
  7164. if (!path) {
  7165. ret = -ENOMEM;
  7166. fscrypt_free_filename(&fname);
  7167. goto out;
  7168. }
  7169. /*
  7170. * We have two inodes to update in the log, the old directory and
  7171. * the inode that got renamed, so we must pin the log to prevent
  7172. * anyone from syncing the log until we have updated both inodes
  7173. * in the log.
  7174. */
  7175. ret = join_running_log_trans(root);
  7176. /*
  7177. * At least one of the inodes was logged before, so this should
  7178. * not fail, but if it does, it's not serious, just bail out and
  7179. * mark the log for a full commit.
  7180. */
  7181. if (WARN_ON_ONCE(ret < 0)) {
  7182. btrfs_free_path(path);
  7183. fscrypt_free_filename(&fname);
  7184. goto out;
  7185. }
  7186. log_pinned = true;
  7187. /*
  7188. * Other concurrent task might be logging the old directory,
  7189. * as it can be triggered when logging other inode that had or
  7190. * still has a dentry in the old directory. We lock the old
  7191. * directory's log_mutex to ensure the deletion of the old
  7192. * name is persisted, because during directory logging we
  7193. * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of
  7194. * the old name's dir index item is in the delayed items, so
  7195. * it could be missed by an in progress directory logging.
  7196. */
  7197. mutex_lock(&old_dir->log_mutex);
  7198. ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir),
  7199. &fname.disk_name, old_dir_index);
  7200. if (ret > 0) {
  7201. /*
  7202. * The dentry does not exist in the log, so record its
  7203. * deletion.
  7204. */
  7205. btrfs_release_path(path);
  7206. ret = insert_dir_log_key(trans, log, path,
  7207. btrfs_ino(old_dir),
  7208. old_dir_index, old_dir_index);
  7209. }
  7210. mutex_unlock(&old_dir->log_mutex);
  7211. btrfs_free_path(path);
  7212. fscrypt_free_filename(&fname);
  7213. if (ret < 0)
  7214. goto out;
  7215. }
  7216. /*
  7217. * We don't care about the return value. If we fail to log the new name
  7218. * then we know the next attempt to sync the log will fallback to a full
  7219. * transaction commit (due to a call to btrfs_set_log_full_commit()), so
  7220. * we don't need to worry about getting a log committed that has an
  7221. * inconsistent state after a rename operation.
  7222. */
  7223. btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
  7224. ASSERT(list_empty(&ctx.conflict_inodes));
  7225. out:
  7226. /*
  7227. * If an error happened mark the log for a full commit because it's not
  7228. * consistent and up to date or we couldn't find out if one of the
  7229. * inodes was logged before in this transaction. Do it before unpinning
  7230. * the log, to avoid any races with someone else trying to commit it.
  7231. */
  7232. if (ret < 0)
  7233. btrfs_set_log_full_commit(trans);
  7234. if (log_pinned)
  7235. btrfs_end_log_trans(root);
  7236. free_extent_buffer(ctx.scratch_eb);
  7237. }