tree-log.c 155 KB

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