tree-log.c 156 KB

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