tree-log.c 156 KB

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