inode.c 170 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373
  1. /*
  2. * Copyright (C) 2007 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/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/statfs.h>
  33. #include <linux/compat.h>
  34. #include <linux/bit_spinlock.h>
  35. #include <linux/xattr.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/falloc.h>
  38. #include <linux/slab.h>
  39. #include "compat.h"
  40. #include "ctree.h"
  41. #include "disk-io.h"
  42. #include "transaction.h"
  43. #include "btrfs_inode.h"
  44. #include "ioctl.h"
  45. #include "print-tree.h"
  46. #include "volumes.h"
  47. #include "ordered-data.h"
  48. #include "xattr.h"
  49. #include "tree-log.h"
  50. #include "compression.h"
  51. #include "locking.h"
  52. struct btrfs_iget_args {
  53. u64 ino;
  54. struct btrfs_root *root;
  55. };
  56. static const struct inode_operations btrfs_dir_inode_operations;
  57. static const struct inode_operations btrfs_symlink_inode_operations;
  58. static const struct inode_operations btrfs_dir_ro_inode_operations;
  59. static const struct inode_operations btrfs_special_inode_operations;
  60. static const struct inode_operations btrfs_file_inode_operations;
  61. static const struct address_space_operations btrfs_aops;
  62. static const struct address_space_operations btrfs_symlink_aops;
  63. static const struct file_operations btrfs_dir_file_operations;
  64. static struct extent_io_ops btrfs_extent_io_ops;
  65. static struct kmem_cache *btrfs_inode_cachep;
  66. struct kmem_cache *btrfs_trans_handle_cachep;
  67. struct kmem_cache *btrfs_transaction_cachep;
  68. struct kmem_cache *btrfs_path_cachep;
  69. #define S_SHIFT 12
  70. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  71. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  72. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  73. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  74. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  75. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  76. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  77. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  78. };
  79. static void btrfs_truncate(struct inode *inode);
  80. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  81. static noinline int cow_file_range(struct inode *inode,
  82. struct page *locked_page,
  83. u64 start, u64 end, int *page_started,
  84. unsigned long *nr_written, int unlock);
  85. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  86. struct inode *inode, struct inode *dir)
  87. {
  88. int err;
  89. err = btrfs_init_acl(trans, inode, dir);
  90. if (!err)
  91. err = btrfs_xattr_security_init(trans, inode, dir);
  92. return err;
  93. }
  94. /*
  95. * this does all the hard work for inserting an inline extent into
  96. * the btree. The caller should have done a btrfs_drop_extents so that
  97. * no overlapping inline items exist in the btree
  98. */
  99. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root, struct inode *inode,
  101. u64 start, size_t size, size_t compressed_size,
  102. struct page **compressed_pages)
  103. {
  104. struct btrfs_key key;
  105. struct btrfs_path *path;
  106. struct extent_buffer *leaf;
  107. struct page *page = NULL;
  108. char *kaddr;
  109. unsigned long ptr;
  110. struct btrfs_file_extent_item *ei;
  111. int err = 0;
  112. int ret;
  113. size_t cur_size = size;
  114. size_t datasize;
  115. unsigned long offset;
  116. int use_compress = 0;
  117. if (compressed_size && compressed_pages) {
  118. use_compress = 1;
  119. cur_size = compressed_size;
  120. }
  121. path = btrfs_alloc_path();
  122. if (!path)
  123. return -ENOMEM;
  124. path->leave_spinning = 1;
  125. btrfs_set_trans_block_group(trans, inode);
  126. key.objectid = inode->i_ino;
  127. key.offset = start;
  128. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  129. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  130. inode_add_bytes(inode, size);
  131. ret = btrfs_insert_empty_item(trans, root, path, &key,
  132. datasize);
  133. BUG_ON(ret);
  134. if (ret) {
  135. err = ret;
  136. goto fail;
  137. }
  138. leaf = path->nodes[0];
  139. ei = btrfs_item_ptr(leaf, path->slots[0],
  140. struct btrfs_file_extent_item);
  141. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  142. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  143. btrfs_set_file_extent_encryption(leaf, ei, 0);
  144. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  145. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  146. ptr = btrfs_file_extent_inline_start(ei);
  147. if (use_compress) {
  148. struct page *cpage;
  149. int i = 0;
  150. while (compressed_size > 0) {
  151. cpage = compressed_pages[i];
  152. cur_size = min_t(unsigned long, compressed_size,
  153. PAGE_CACHE_SIZE);
  154. kaddr = kmap_atomic(cpage, KM_USER0);
  155. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  156. kunmap_atomic(kaddr, KM_USER0);
  157. i++;
  158. ptr += cur_size;
  159. compressed_size -= cur_size;
  160. }
  161. btrfs_set_file_extent_compression(leaf, ei,
  162. BTRFS_COMPRESS_ZLIB);
  163. } else {
  164. page = find_get_page(inode->i_mapping,
  165. start >> PAGE_CACHE_SHIFT);
  166. btrfs_set_file_extent_compression(leaf, ei, 0);
  167. kaddr = kmap_atomic(page, KM_USER0);
  168. offset = start & (PAGE_CACHE_SIZE - 1);
  169. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  170. kunmap_atomic(kaddr, KM_USER0);
  171. page_cache_release(page);
  172. }
  173. btrfs_mark_buffer_dirty(leaf);
  174. btrfs_free_path(path);
  175. /*
  176. * we're an inline extent, so nobody can
  177. * extend the file past i_size without locking
  178. * a page we already have locked.
  179. *
  180. * We must do any isize and inode updates
  181. * before we unlock the pages. Otherwise we
  182. * could end up racing with unlink.
  183. */
  184. BTRFS_I(inode)->disk_i_size = inode->i_size;
  185. btrfs_update_inode(trans, root, inode);
  186. return 0;
  187. fail:
  188. btrfs_free_path(path);
  189. return err;
  190. }
  191. /*
  192. * conditionally insert an inline extent into the file. This
  193. * does the checks required to make sure the data is small enough
  194. * to fit as an inline extent.
  195. */
  196. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  197. struct btrfs_root *root,
  198. struct inode *inode, u64 start, u64 end,
  199. size_t compressed_size,
  200. struct page **compressed_pages)
  201. {
  202. u64 isize = i_size_read(inode);
  203. u64 actual_end = min(end + 1, isize);
  204. u64 inline_len = actual_end - start;
  205. u64 aligned_end = (end + root->sectorsize - 1) &
  206. ~((u64)root->sectorsize - 1);
  207. u64 hint_byte;
  208. u64 data_len = inline_len;
  209. int ret;
  210. if (compressed_size)
  211. data_len = compressed_size;
  212. if (start > 0 ||
  213. actual_end >= PAGE_CACHE_SIZE ||
  214. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  215. (!compressed_size &&
  216. (actual_end & (root->sectorsize - 1)) == 0) ||
  217. end + 1 < isize ||
  218. data_len > root->fs_info->max_inline) {
  219. return 1;
  220. }
  221. ret = btrfs_drop_extents(trans, inode, start, aligned_end,
  222. &hint_byte, 1);
  223. BUG_ON(ret);
  224. if (isize > actual_end)
  225. inline_len = min_t(u64, isize, actual_end);
  226. ret = insert_inline_extent(trans, root, inode, start,
  227. inline_len, compressed_size,
  228. compressed_pages);
  229. BUG_ON(ret);
  230. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  231. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  232. return 0;
  233. }
  234. struct async_extent {
  235. u64 start;
  236. u64 ram_size;
  237. u64 compressed_size;
  238. struct page **pages;
  239. unsigned long nr_pages;
  240. struct list_head list;
  241. };
  242. struct async_cow {
  243. struct inode *inode;
  244. struct btrfs_root *root;
  245. struct page *locked_page;
  246. u64 start;
  247. u64 end;
  248. struct list_head extents;
  249. struct btrfs_work work;
  250. };
  251. static noinline int add_async_extent(struct async_cow *cow,
  252. u64 start, u64 ram_size,
  253. u64 compressed_size,
  254. struct page **pages,
  255. unsigned long nr_pages)
  256. {
  257. struct async_extent *async_extent;
  258. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  259. async_extent->start = start;
  260. async_extent->ram_size = ram_size;
  261. async_extent->compressed_size = compressed_size;
  262. async_extent->pages = pages;
  263. async_extent->nr_pages = nr_pages;
  264. list_add_tail(&async_extent->list, &cow->extents);
  265. return 0;
  266. }
  267. /*
  268. * we create compressed extents in two phases. The first
  269. * phase compresses a range of pages that have already been
  270. * locked (both pages and state bits are locked).
  271. *
  272. * This is done inside an ordered work queue, and the compression
  273. * is spread across many cpus. The actual IO submission is step
  274. * two, and the ordered work queue takes care of making sure that
  275. * happens in the same order things were put onto the queue by
  276. * writepages and friends.
  277. *
  278. * If this code finds it can't get good compression, it puts an
  279. * entry onto the work queue to write the uncompressed bytes. This
  280. * makes sure that both compressed inodes and uncompressed inodes
  281. * are written in the same order that pdflush sent them down.
  282. */
  283. static noinline int compress_file_range(struct inode *inode,
  284. struct page *locked_page,
  285. u64 start, u64 end,
  286. struct async_cow *async_cow,
  287. int *num_added)
  288. {
  289. struct btrfs_root *root = BTRFS_I(inode)->root;
  290. struct btrfs_trans_handle *trans;
  291. u64 num_bytes;
  292. u64 orig_start;
  293. u64 disk_num_bytes;
  294. u64 blocksize = root->sectorsize;
  295. u64 actual_end;
  296. u64 isize = i_size_read(inode);
  297. int ret = 0;
  298. struct page **pages = NULL;
  299. unsigned long nr_pages;
  300. unsigned long nr_pages_ret = 0;
  301. unsigned long total_compressed = 0;
  302. unsigned long total_in = 0;
  303. unsigned long max_compressed = 128 * 1024;
  304. unsigned long max_uncompressed = 128 * 1024;
  305. int i;
  306. int will_compress;
  307. orig_start = start;
  308. actual_end = min_t(u64, isize, end + 1);
  309. again:
  310. will_compress = 0;
  311. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  312. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  313. /*
  314. * we don't want to send crud past the end of i_size through
  315. * compression, that's just a waste of CPU time. So, if the
  316. * end of the file is before the start of our current
  317. * requested range of bytes, we bail out to the uncompressed
  318. * cleanup code that can deal with all of this.
  319. *
  320. * It isn't really the fastest way to fix things, but this is a
  321. * very uncommon corner.
  322. */
  323. if (actual_end <= start)
  324. goto cleanup_and_bail_uncompressed;
  325. total_compressed = actual_end - start;
  326. /* we want to make sure that amount of ram required to uncompress
  327. * an extent is reasonable, so we limit the total size in ram
  328. * of a compressed extent to 128k. This is a crucial number
  329. * because it also controls how easily we can spread reads across
  330. * cpus for decompression.
  331. *
  332. * We also want to make sure the amount of IO required to do
  333. * a random read is reasonably small, so we limit the size of
  334. * a compressed extent to 128k.
  335. */
  336. total_compressed = min(total_compressed, max_uncompressed);
  337. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  338. num_bytes = max(blocksize, num_bytes);
  339. disk_num_bytes = num_bytes;
  340. total_in = 0;
  341. ret = 0;
  342. /*
  343. * we do compression for mount -o compress and when the
  344. * inode has not been flagged as nocompress. This flag can
  345. * change at any time if we discover bad compression ratios.
  346. */
  347. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  348. (btrfs_test_opt(root, COMPRESS) ||
  349. (BTRFS_I(inode)->force_compress))) {
  350. WARN_ON(pages);
  351. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  352. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  353. total_compressed, pages,
  354. nr_pages, &nr_pages_ret,
  355. &total_in,
  356. &total_compressed,
  357. max_compressed);
  358. if (!ret) {
  359. unsigned long offset = total_compressed &
  360. (PAGE_CACHE_SIZE - 1);
  361. struct page *page = pages[nr_pages_ret - 1];
  362. char *kaddr;
  363. /* zero the tail end of the last page, we might be
  364. * sending it down to disk
  365. */
  366. if (offset) {
  367. kaddr = kmap_atomic(page, KM_USER0);
  368. memset(kaddr + offset, 0,
  369. PAGE_CACHE_SIZE - offset);
  370. kunmap_atomic(kaddr, KM_USER0);
  371. }
  372. will_compress = 1;
  373. }
  374. }
  375. if (start == 0) {
  376. trans = btrfs_join_transaction(root, 1);
  377. BUG_ON(!trans);
  378. btrfs_set_trans_block_group(trans, inode);
  379. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  380. /* lets try to make an inline extent */
  381. if (ret || total_in < (actual_end - start)) {
  382. /* we didn't compress the entire range, try
  383. * to make an uncompressed inline extent.
  384. */
  385. ret = cow_file_range_inline(trans, root, inode,
  386. start, end, 0, NULL);
  387. } else {
  388. /* try making a compressed inline extent */
  389. ret = cow_file_range_inline(trans, root, inode,
  390. start, end,
  391. total_compressed, pages);
  392. }
  393. if (ret == 0) {
  394. /*
  395. * inline extent creation worked, we don't need
  396. * to create any more async work items. Unlock
  397. * and free up our temp pages.
  398. */
  399. extent_clear_unlock_delalloc(inode,
  400. &BTRFS_I(inode)->io_tree,
  401. start, end, NULL,
  402. EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  403. EXTENT_CLEAR_DELALLOC |
  404. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
  405. btrfs_end_transaction(trans, root);
  406. goto free_pages_out;
  407. }
  408. btrfs_end_transaction(trans, root);
  409. }
  410. if (will_compress) {
  411. /*
  412. * we aren't doing an inline extent round the compressed size
  413. * up to a block size boundary so the allocator does sane
  414. * things
  415. */
  416. total_compressed = (total_compressed + blocksize - 1) &
  417. ~(blocksize - 1);
  418. /*
  419. * one last check to make sure the compression is really a
  420. * win, compare the page count read with the blocks on disk
  421. */
  422. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  423. ~(PAGE_CACHE_SIZE - 1);
  424. if (total_compressed >= total_in) {
  425. will_compress = 0;
  426. } else {
  427. disk_num_bytes = total_compressed;
  428. num_bytes = total_in;
  429. }
  430. }
  431. if (!will_compress && pages) {
  432. /*
  433. * the compression code ran but failed to make things smaller,
  434. * free any pages it allocated and our page pointer array
  435. */
  436. for (i = 0; i < nr_pages_ret; i++) {
  437. WARN_ON(pages[i]->mapping);
  438. page_cache_release(pages[i]);
  439. }
  440. kfree(pages);
  441. pages = NULL;
  442. total_compressed = 0;
  443. nr_pages_ret = 0;
  444. /* flag the file so we don't compress in the future */
  445. if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
  446. !(BTRFS_I(inode)->force_compress)) {
  447. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  448. }
  449. }
  450. if (will_compress) {
  451. *num_added += 1;
  452. /* the async work queues will take care of doing actual
  453. * allocation on disk for these compressed pages,
  454. * and will submit them to the elevator.
  455. */
  456. add_async_extent(async_cow, start, num_bytes,
  457. total_compressed, pages, nr_pages_ret);
  458. if (start + num_bytes < end && start + num_bytes < actual_end) {
  459. start += num_bytes;
  460. pages = NULL;
  461. cond_resched();
  462. goto again;
  463. }
  464. } else {
  465. cleanup_and_bail_uncompressed:
  466. /*
  467. * No compression, but we still need to write the pages in
  468. * the file we've been given so far. redirty the locked
  469. * page if it corresponds to our extent and set things up
  470. * for the async work queue to run cow_file_range to do
  471. * the normal delalloc dance
  472. */
  473. if (page_offset(locked_page) >= start &&
  474. page_offset(locked_page) <= end) {
  475. __set_page_dirty_nobuffers(locked_page);
  476. /* unlocked later on in the async handlers */
  477. }
  478. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  479. *num_added += 1;
  480. }
  481. out:
  482. return 0;
  483. free_pages_out:
  484. for (i = 0; i < nr_pages_ret; i++) {
  485. WARN_ON(pages[i]->mapping);
  486. page_cache_release(pages[i]);
  487. }
  488. kfree(pages);
  489. goto out;
  490. }
  491. /*
  492. * phase two of compressed writeback. This is the ordered portion
  493. * of the code, which only gets called in the order the work was
  494. * queued. We walk all the async extents created by compress_file_range
  495. * and send them down to the disk.
  496. */
  497. static noinline int submit_compressed_extents(struct inode *inode,
  498. struct async_cow *async_cow)
  499. {
  500. struct async_extent *async_extent;
  501. u64 alloc_hint = 0;
  502. struct btrfs_trans_handle *trans;
  503. struct btrfs_key ins;
  504. struct extent_map *em;
  505. struct btrfs_root *root = BTRFS_I(inode)->root;
  506. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  507. struct extent_io_tree *io_tree;
  508. int ret = 0;
  509. if (list_empty(&async_cow->extents))
  510. return 0;
  511. while (!list_empty(&async_cow->extents)) {
  512. async_extent = list_entry(async_cow->extents.next,
  513. struct async_extent, list);
  514. list_del(&async_extent->list);
  515. io_tree = &BTRFS_I(inode)->io_tree;
  516. retry:
  517. /* did the compression code fall back to uncompressed IO? */
  518. if (!async_extent->pages) {
  519. int page_started = 0;
  520. unsigned long nr_written = 0;
  521. lock_extent(io_tree, async_extent->start,
  522. async_extent->start +
  523. async_extent->ram_size - 1, GFP_NOFS);
  524. /* allocate blocks */
  525. ret = cow_file_range(inode, async_cow->locked_page,
  526. async_extent->start,
  527. async_extent->start +
  528. async_extent->ram_size - 1,
  529. &page_started, &nr_written, 0);
  530. /*
  531. * if page_started, cow_file_range inserted an
  532. * inline extent and took care of all the unlocking
  533. * and IO for us. Otherwise, we need to submit
  534. * all those pages down to the drive.
  535. */
  536. if (!page_started && !ret)
  537. extent_write_locked_range(io_tree,
  538. inode, async_extent->start,
  539. async_extent->start +
  540. async_extent->ram_size - 1,
  541. btrfs_get_extent,
  542. WB_SYNC_ALL);
  543. kfree(async_extent);
  544. cond_resched();
  545. continue;
  546. }
  547. lock_extent(io_tree, async_extent->start,
  548. async_extent->start + async_extent->ram_size - 1,
  549. GFP_NOFS);
  550. trans = btrfs_join_transaction(root, 1);
  551. ret = btrfs_reserve_extent(trans, root,
  552. async_extent->compressed_size,
  553. async_extent->compressed_size,
  554. 0, alloc_hint,
  555. (u64)-1, &ins, 1);
  556. btrfs_end_transaction(trans, root);
  557. if (ret) {
  558. int i;
  559. for (i = 0; i < async_extent->nr_pages; i++) {
  560. WARN_ON(async_extent->pages[i]->mapping);
  561. page_cache_release(async_extent->pages[i]);
  562. }
  563. kfree(async_extent->pages);
  564. async_extent->nr_pages = 0;
  565. async_extent->pages = NULL;
  566. unlock_extent(io_tree, async_extent->start,
  567. async_extent->start +
  568. async_extent->ram_size - 1, GFP_NOFS);
  569. goto retry;
  570. }
  571. /*
  572. * here we're doing allocation and writeback of the
  573. * compressed pages
  574. */
  575. btrfs_drop_extent_cache(inode, async_extent->start,
  576. async_extent->start +
  577. async_extent->ram_size - 1, 0);
  578. em = alloc_extent_map(GFP_NOFS);
  579. em->start = async_extent->start;
  580. em->len = async_extent->ram_size;
  581. em->orig_start = em->start;
  582. em->block_start = ins.objectid;
  583. em->block_len = ins.offset;
  584. em->bdev = root->fs_info->fs_devices->latest_bdev;
  585. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  586. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  587. while (1) {
  588. write_lock(&em_tree->lock);
  589. ret = add_extent_mapping(em_tree, em);
  590. write_unlock(&em_tree->lock);
  591. if (ret != -EEXIST) {
  592. free_extent_map(em);
  593. break;
  594. }
  595. btrfs_drop_extent_cache(inode, async_extent->start,
  596. async_extent->start +
  597. async_extent->ram_size - 1, 0);
  598. }
  599. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  600. ins.objectid,
  601. async_extent->ram_size,
  602. ins.offset,
  603. BTRFS_ORDERED_COMPRESSED);
  604. BUG_ON(ret);
  605. /*
  606. * clear dirty, set writeback and unlock the pages.
  607. */
  608. extent_clear_unlock_delalloc(inode,
  609. &BTRFS_I(inode)->io_tree,
  610. async_extent->start,
  611. async_extent->start +
  612. async_extent->ram_size - 1,
  613. NULL, EXTENT_CLEAR_UNLOCK_PAGE |
  614. EXTENT_CLEAR_UNLOCK |
  615. EXTENT_CLEAR_DELALLOC |
  616. EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
  617. ret = btrfs_submit_compressed_write(inode,
  618. async_extent->start,
  619. async_extent->ram_size,
  620. ins.objectid,
  621. ins.offset, async_extent->pages,
  622. async_extent->nr_pages);
  623. BUG_ON(ret);
  624. alloc_hint = ins.objectid + ins.offset;
  625. kfree(async_extent);
  626. cond_resched();
  627. }
  628. return 0;
  629. }
  630. /*
  631. * when extent_io.c finds a delayed allocation range in the file,
  632. * the call backs end up in this code. The basic idea is to
  633. * allocate extents on disk for the range, and create ordered data structs
  634. * in ram to track those extents.
  635. *
  636. * locked_page is the page that writepage had locked already. We use
  637. * it to make sure we don't do extra locks or unlocks.
  638. *
  639. * *page_started is set to one if we unlock locked_page and do everything
  640. * required to start IO on it. It may be clean and already done with
  641. * IO when we return.
  642. */
  643. static noinline int cow_file_range(struct inode *inode,
  644. struct page *locked_page,
  645. u64 start, u64 end, int *page_started,
  646. unsigned long *nr_written,
  647. int unlock)
  648. {
  649. struct btrfs_root *root = BTRFS_I(inode)->root;
  650. struct btrfs_trans_handle *trans;
  651. u64 alloc_hint = 0;
  652. u64 num_bytes;
  653. unsigned long ram_size;
  654. u64 disk_num_bytes;
  655. u64 cur_alloc_size;
  656. u64 blocksize = root->sectorsize;
  657. u64 actual_end;
  658. u64 isize = i_size_read(inode);
  659. struct btrfs_key ins;
  660. struct extent_map *em;
  661. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  662. int ret = 0;
  663. trans = btrfs_join_transaction(root, 1);
  664. BUG_ON(!trans);
  665. btrfs_set_trans_block_group(trans, inode);
  666. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  667. actual_end = min_t(u64, isize, end + 1);
  668. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  669. num_bytes = max(blocksize, num_bytes);
  670. disk_num_bytes = num_bytes;
  671. ret = 0;
  672. if (start == 0) {
  673. /* lets try to make an inline extent */
  674. ret = cow_file_range_inline(trans, root, inode,
  675. start, end, 0, NULL);
  676. if (ret == 0) {
  677. extent_clear_unlock_delalloc(inode,
  678. &BTRFS_I(inode)->io_tree,
  679. start, end, NULL,
  680. EXTENT_CLEAR_UNLOCK_PAGE |
  681. EXTENT_CLEAR_UNLOCK |
  682. EXTENT_CLEAR_DELALLOC |
  683. EXTENT_CLEAR_DIRTY |
  684. EXTENT_SET_WRITEBACK |
  685. EXTENT_END_WRITEBACK);
  686. *nr_written = *nr_written +
  687. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  688. *page_started = 1;
  689. ret = 0;
  690. goto out;
  691. }
  692. }
  693. BUG_ON(disk_num_bytes >
  694. btrfs_super_total_bytes(&root->fs_info->super_copy));
  695. read_lock(&BTRFS_I(inode)->extent_tree.lock);
  696. em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
  697. start, num_bytes);
  698. if (em) {
  699. /*
  700. * if block start isn't an actual block number then find the
  701. * first block in this inode and use that as a hint. If that
  702. * block is also bogus then just don't worry about it.
  703. */
  704. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  705. free_extent_map(em);
  706. em = search_extent_mapping(em_tree, 0, 0);
  707. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  708. alloc_hint = em->block_start;
  709. if (em)
  710. free_extent_map(em);
  711. } else {
  712. alloc_hint = em->block_start;
  713. free_extent_map(em);
  714. }
  715. }
  716. read_unlock(&BTRFS_I(inode)->extent_tree.lock);
  717. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  718. while (disk_num_bytes > 0) {
  719. unsigned long op;
  720. cur_alloc_size = disk_num_bytes;
  721. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  722. root->sectorsize, 0, alloc_hint,
  723. (u64)-1, &ins, 1);
  724. BUG_ON(ret);
  725. em = alloc_extent_map(GFP_NOFS);
  726. em->start = start;
  727. em->orig_start = em->start;
  728. ram_size = ins.offset;
  729. em->len = ins.offset;
  730. em->block_start = ins.objectid;
  731. em->block_len = ins.offset;
  732. em->bdev = root->fs_info->fs_devices->latest_bdev;
  733. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  734. while (1) {
  735. write_lock(&em_tree->lock);
  736. ret = add_extent_mapping(em_tree, em);
  737. write_unlock(&em_tree->lock);
  738. if (ret != -EEXIST) {
  739. free_extent_map(em);
  740. break;
  741. }
  742. btrfs_drop_extent_cache(inode, start,
  743. start + ram_size - 1, 0);
  744. }
  745. cur_alloc_size = ins.offset;
  746. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  747. ram_size, cur_alloc_size, 0);
  748. BUG_ON(ret);
  749. if (root->root_key.objectid ==
  750. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  751. ret = btrfs_reloc_clone_csums(inode, start,
  752. cur_alloc_size);
  753. BUG_ON(ret);
  754. }
  755. if (disk_num_bytes < cur_alloc_size)
  756. break;
  757. /* we're not doing compressed IO, don't unlock the first
  758. * page (which the caller expects to stay locked), don't
  759. * clear any dirty bits and don't set any writeback bits
  760. *
  761. * Do set the Private2 bit so we know this page was properly
  762. * setup for writepage
  763. */
  764. op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
  765. op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  766. EXTENT_SET_PRIVATE2;
  767. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  768. start, start + ram_size - 1,
  769. locked_page, op);
  770. disk_num_bytes -= cur_alloc_size;
  771. num_bytes -= cur_alloc_size;
  772. alloc_hint = ins.objectid + ins.offset;
  773. start += cur_alloc_size;
  774. }
  775. out:
  776. ret = 0;
  777. btrfs_end_transaction(trans, root);
  778. return ret;
  779. }
  780. /*
  781. * work queue call back to started compression on a file and pages
  782. */
  783. static noinline void async_cow_start(struct btrfs_work *work)
  784. {
  785. struct async_cow *async_cow;
  786. int num_added = 0;
  787. async_cow = container_of(work, struct async_cow, work);
  788. compress_file_range(async_cow->inode, async_cow->locked_page,
  789. async_cow->start, async_cow->end, async_cow,
  790. &num_added);
  791. if (num_added == 0)
  792. async_cow->inode = NULL;
  793. }
  794. /*
  795. * work queue call back to submit previously compressed pages
  796. */
  797. static noinline void async_cow_submit(struct btrfs_work *work)
  798. {
  799. struct async_cow *async_cow;
  800. struct btrfs_root *root;
  801. unsigned long nr_pages;
  802. async_cow = container_of(work, struct async_cow, work);
  803. root = async_cow->root;
  804. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  805. PAGE_CACHE_SHIFT;
  806. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  807. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  808. 5 * 1042 * 1024 &&
  809. waitqueue_active(&root->fs_info->async_submit_wait))
  810. wake_up(&root->fs_info->async_submit_wait);
  811. if (async_cow->inode)
  812. submit_compressed_extents(async_cow->inode, async_cow);
  813. }
  814. static noinline void async_cow_free(struct btrfs_work *work)
  815. {
  816. struct async_cow *async_cow;
  817. async_cow = container_of(work, struct async_cow, work);
  818. kfree(async_cow);
  819. }
  820. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  821. u64 start, u64 end, int *page_started,
  822. unsigned long *nr_written)
  823. {
  824. struct async_cow *async_cow;
  825. struct btrfs_root *root = BTRFS_I(inode)->root;
  826. unsigned long nr_pages;
  827. u64 cur_end;
  828. int limit = 10 * 1024 * 1042;
  829. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  830. 1, 0, NULL, GFP_NOFS);
  831. while (start < end) {
  832. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  833. async_cow->inode = inode;
  834. async_cow->root = root;
  835. async_cow->locked_page = locked_page;
  836. async_cow->start = start;
  837. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  838. cur_end = end;
  839. else
  840. cur_end = min(end, start + 512 * 1024 - 1);
  841. async_cow->end = cur_end;
  842. INIT_LIST_HEAD(&async_cow->extents);
  843. async_cow->work.func = async_cow_start;
  844. async_cow->work.ordered_func = async_cow_submit;
  845. async_cow->work.ordered_free = async_cow_free;
  846. async_cow->work.flags = 0;
  847. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  848. PAGE_CACHE_SHIFT;
  849. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  850. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  851. &async_cow->work);
  852. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  853. wait_event(root->fs_info->async_submit_wait,
  854. (atomic_read(&root->fs_info->async_delalloc_pages) <
  855. limit));
  856. }
  857. while (atomic_read(&root->fs_info->async_submit_draining) &&
  858. atomic_read(&root->fs_info->async_delalloc_pages)) {
  859. wait_event(root->fs_info->async_submit_wait,
  860. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  861. 0));
  862. }
  863. *nr_written += nr_pages;
  864. start = cur_end + 1;
  865. }
  866. *page_started = 1;
  867. return 0;
  868. }
  869. static noinline int csum_exist_in_range(struct btrfs_root *root,
  870. u64 bytenr, u64 num_bytes)
  871. {
  872. int ret;
  873. struct btrfs_ordered_sum *sums;
  874. LIST_HEAD(list);
  875. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  876. bytenr + num_bytes - 1, &list);
  877. if (ret == 0 && list_empty(&list))
  878. return 0;
  879. while (!list_empty(&list)) {
  880. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  881. list_del(&sums->list);
  882. kfree(sums);
  883. }
  884. return 1;
  885. }
  886. /*
  887. * when nowcow writeback call back. This checks for snapshots or COW copies
  888. * of the extents that exist in the file, and COWs the file as required.
  889. *
  890. * If no cow copies or snapshots exist, we write directly to the existing
  891. * blocks on disk
  892. */
  893. static noinline int run_delalloc_nocow(struct inode *inode,
  894. struct page *locked_page,
  895. u64 start, u64 end, int *page_started, int force,
  896. unsigned long *nr_written)
  897. {
  898. struct btrfs_root *root = BTRFS_I(inode)->root;
  899. struct btrfs_trans_handle *trans;
  900. struct extent_buffer *leaf;
  901. struct btrfs_path *path;
  902. struct btrfs_file_extent_item *fi;
  903. struct btrfs_key found_key;
  904. u64 cow_start;
  905. u64 cur_offset;
  906. u64 extent_end;
  907. u64 extent_offset;
  908. u64 disk_bytenr;
  909. u64 num_bytes;
  910. int extent_type;
  911. int ret;
  912. int type;
  913. int nocow;
  914. int check_prev = 1;
  915. path = btrfs_alloc_path();
  916. BUG_ON(!path);
  917. trans = btrfs_join_transaction(root, 1);
  918. BUG_ON(!trans);
  919. cow_start = (u64)-1;
  920. cur_offset = start;
  921. while (1) {
  922. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  923. cur_offset, 0);
  924. BUG_ON(ret < 0);
  925. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  926. leaf = path->nodes[0];
  927. btrfs_item_key_to_cpu(leaf, &found_key,
  928. path->slots[0] - 1);
  929. if (found_key.objectid == inode->i_ino &&
  930. found_key.type == BTRFS_EXTENT_DATA_KEY)
  931. path->slots[0]--;
  932. }
  933. check_prev = 0;
  934. next_slot:
  935. leaf = path->nodes[0];
  936. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  937. ret = btrfs_next_leaf(root, path);
  938. if (ret < 0)
  939. BUG_ON(1);
  940. if (ret > 0)
  941. break;
  942. leaf = path->nodes[0];
  943. }
  944. nocow = 0;
  945. disk_bytenr = 0;
  946. num_bytes = 0;
  947. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  948. if (found_key.objectid > inode->i_ino ||
  949. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  950. found_key.offset > end)
  951. break;
  952. if (found_key.offset > cur_offset) {
  953. extent_end = found_key.offset;
  954. extent_type = 0;
  955. goto out_check;
  956. }
  957. fi = btrfs_item_ptr(leaf, path->slots[0],
  958. struct btrfs_file_extent_item);
  959. extent_type = btrfs_file_extent_type(leaf, fi);
  960. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  961. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  962. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  963. extent_offset = btrfs_file_extent_offset(leaf, fi);
  964. extent_end = found_key.offset +
  965. btrfs_file_extent_num_bytes(leaf, fi);
  966. if (extent_end <= start) {
  967. path->slots[0]++;
  968. goto next_slot;
  969. }
  970. if (disk_bytenr == 0)
  971. goto out_check;
  972. if (btrfs_file_extent_compression(leaf, fi) ||
  973. btrfs_file_extent_encryption(leaf, fi) ||
  974. btrfs_file_extent_other_encoding(leaf, fi))
  975. goto out_check;
  976. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  977. goto out_check;
  978. if (btrfs_extent_readonly(root, disk_bytenr))
  979. goto out_check;
  980. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  981. found_key.offset -
  982. extent_offset, disk_bytenr))
  983. goto out_check;
  984. disk_bytenr += extent_offset;
  985. disk_bytenr += cur_offset - found_key.offset;
  986. num_bytes = min(end + 1, extent_end) - cur_offset;
  987. /*
  988. * force cow if csum exists in the range.
  989. * this ensure that csum for a given extent are
  990. * either valid or do not exist.
  991. */
  992. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  993. goto out_check;
  994. nocow = 1;
  995. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  996. extent_end = found_key.offset +
  997. btrfs_file_extent_inline_len(leaf, fi);
  998. extent_end = ALIGN(extent_end, root->sectorsize);
  999. } else {
  1000. BUG_ON(1);
  1001. }
  1002. out_check:
  1003. if (extent_end <= start) {
  1004. path->slots[0]++;
  1005. goto next_slot;
  1006. }
  1007. if (!nocow) {
  1008. if (cow_start == (u64)-1)
  1009. cow_start = cur_offset;
  1010. cur_offset = extent_end;
  1011. if (cur_offset > end)
  1012. break;
  1013. path->slots[0]++;
  1014. goto next_slot;
  1015. }
  1016. btrfs_release_path(root, path);
  1017. if (cow_start != (u64)-1) {
  1018. ret = cow_file_range(inode, locked_page, cow_start,
  1019. found_key.offset - 1, page_started,
  1020. nr_written, 1);
  1021. BUG_ON(ret);
  1022. cow_start = (u64)-1;
  1023. }
  1024. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1025. struct extent_map *em;
  1026. struct extent_map_tree *em_tree;
  1027. em_tree = &BTRFS_I(inode)->extent_tree;
  1028. em = alloc_extent_map(GFP_NOFS);
  1029. em->start = cur_offset;
  1030. em->orig_start = em->start;
  1031. em->len = num_bytes;
  1032. em->block_len = num_bytes;
  1033. em->block_start = disk_bytenr;
  1034. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1035. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1036. while (1) {
  1037. write_lock(&em_tree->lock);
  1038. ret = add_extent_mapping(em_tree, em);
  1039. write_unlock(&em_tree->lock);
  1040. if (ret != -EEXIST) {
  1041. free_extent_map(em);
  1042. break;
  1043. }
  1044. btrfs_drop_extent_cache(inode, em->start,
  1045. em->start + em->len - 1, 0);
  1046. }
  1047. type = BTRFS_ORDERED_PREALLOC;
  1048. } else {
  1049. type = BTRFS_ORDERED_NOCOW;
  1050. }
  1051. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1052. num_bytes, num_bytes, type);
  1053. BUG_ON(ret);
  1054. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  1055. cur_offset, cur_offset + num_bytes - 1,
  1056. locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
  1057. EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  1058. EXTENT_SET_PRIVATE2);
  1059. cur_offset = extent_end;
  1060. if (cur_offset > end)
  1061. break;
  1062. }
  1063. btrfs_release_path(root, path);
  1064. if (cur_offset <= end && cow_start == (u64)-1)
  1065. cow_start = cur_offset;
  1066. if (cow_start != (u64)-1) {
  1067. ret = cow_file_range(inode, locked_page, cow_start, end,
  1068. page_started, nr_written, 1);
  1069. BUG_ON(ret);
  1070. }
  1071. ret = btrfs_end_transaction(trans, root);
  1072. BUG_ON(ret);
  1073. btrfs_free_path(path);
  1074. return 0;
  1075. }
  1076. /*
  1077. * extent_io.c call back to do delayed allocation processing
  1078. */
  1079. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1080. u64 start, u64 end, int *page_started,
  1081. unsigned long *nr_written)
  1082. {
  1083. int ret;
  1084. struct btrfs_root *root = BTRFS_I(inode)->root;
  1085. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1086. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1087. page_started, 1, nr_written);
  1088. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1089. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1090. page_started, 0, nr_written);
  1091. else if (!btrfs_test_opt(root, COMPRESS) &&
  1092. !(BTRFS_I(inode)->force_compress))
  1093. ret = cow_file_range(inode, locked_page, start, end,
  1094. page_started, nr_written, 1);
  1095. else
  1096. ret = cow_file_range_async(inode, locked_page, start, end,
  1097. page_started, nr_written);
  1098. return ret;
  1099. }
  1100. static int btrfs_split_extent_hook(struct inode *inode,
  1101. struct extent_state *orig, u64 split)
  1102. {
  1103. /* not delalloc, ignore it */
  1104. if (!(orig->state & EXTENT_DELALLOC))
  1105. return 0;
  1106. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  1107. return 0;
  1108. }
  1109. /*
  1110. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1111. * extents so we can keep track of new extents that are just merged onto old
  1112. * extents, such as when we are doing sequential writes, so we can properly
  1113. * account for the metadata space we'll need.
  1114. */
  1115. static int btrfs_merge_extent_hook(struct inode *inode,
  1116. struct extent_state *new,
  1117. struct extent_state *other)
  1118. {
  1119. /* not delalloc, ignore it */
  1120. if (!(other->state & EXTENT_DELALLOC))
  1121. return 0;
  1122. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  1123. return 0;
  1124. }
  1125. /*
  1126. * extent_io.c set_bit_hook, used to track delayed allocation
  1127. * bytes in this file, and to maintain the list of inodes that
  1128. * have pending delalloc work to be done.
  1129. */
  1130. static int btrfs_set_bit_hook(struct inode *inode,
  1131. struct extent_state *state, int *bits)
  1132. {
  1133. /*
  1134. * set_bit and clear bit hooks normally require _irqsave/restore
  1135. * but in this case, we are only testeing for the DELALLOC
  1136. * bit, which is only set or cleared with irqs on
  1137. */
  1138. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1139. struct btrfs_root *root = BTRFS_I(inode)->root;
  1140. u64 len = state->end + 1 - state->start;
  1141. if (*bits & EXTENT_FIRST_DELALLOC)
  1142. *bits &= ~EXTENT_FIRST_DELALLOC;
  1143. else
  1144. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  1145. spin_lock(&root->fs_info->delalloc_lock);
  1146. BTRFS_I(inode)->delalloc_bytes += len;
  1147. root->fs_info->delalloc_bytes += len;
  1148. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1149. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1150. &root->fs_info->delalloc_inodes);
  1151. }
  1152. spin_unlock(&root->fs_info->delalloc_lock);
  1153. }
  1154. return 0;
  1155. }
  1156. /*
  1157. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1158. */
  1159. static int btrfs_clear_bit_hook(struct inode *inode,
  1160. struct extent_state *state, int *bits)
  1161. {
  1162. /*
  1163. * set_bit and clear bit hooks normally require _irqsave/restore
  1164. * but in this case, we are only testeing for the DELALLOC
  1165. * bit, which is only set or cleared with irqs on
  1166. */
  1167. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1168. struct btrfs_root *root = BTRFS_I(inode)->root;
  1169. u64 len = state->end + 1 - state->start;
  1170. if (*bits & EXTENT_FIRST_DELALLOC)
  1171. *bits &= ~EXTENT_FIRST_DELALLOC;
  1172. else if (!(*bits & EXTENT_DO_ACCOUNTING))
  1173. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  1174. if (*bits & EXTENT_DO_ACCOUNTING)
  1175. btrfs_delalloc_release_metadata(inode, len);
  1176. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
  1177. btrfs_free_reserved_data_space(inode, len);
  1178. spin_lock(&root->fs_info->delalloc_lock);
  1179. root->fs_info->delalloc_bytes -= len;
  1180. BTRFS_I(inode)->delalloc_bytes -= len;
  1181. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1182. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1183. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1184. }
  1185. spin_unlock(&root->fs_info->delalloc_lock);
  1186. }
  1187. return 0;
  1188. }
  1189. /*
  1190. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1191. * we don't create bios that span stripes or chunks
  1192. */
  1193. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1194. size_t size, struct bio *bio,
  1195. unsigned long bio_flags)
  1196. {
  1197. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1198. struct btrfs_mapping_tree *map_tree;
  1199. u64 logical = (u64)bio->bi_sector << 9;
  1200. u64 length = 0;
  1201. u64 map_length;
  1202. int ret;
  1203. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1204. return 0;
  1205. length = bio->bi_size;
  1206. map_tree = &root->fs_info->mapping_tree;
  1207. map_length = length;
  1208. ret = btrfs_map_block(map_tree, READ, logical,
  1209. &map_length, NULL, 0);
  1210. if (map_length < length + size)
  1211. return 1;
  1212. return 0;
  1213. }
  1214. /*
  1215. * in order to insert checksums into the metadata in large chunks,
  1216. * we wait until bio submission time. All the pages in the bio are
  1217. * checksummed and sums are attached onto the ordered extent record.
  1218. *
  1219. * At IO completion time the cums attached on the ordered extent record
  1220. * are inserted into the btree
  1221. */
  1222. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1223. struct bio *bio, int mirror_num,
  1224. unsigned long bio_flags)
  1225. {
  1226. struct btrfs_root *root = BTRFS_I(inode)->root;
  1227. int ret = 0;
  1228. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1229. BUG_ON(ret);
  1230. return 0;
  1231. }
  1232. /*
  1233. * in order to insert checksums into the metadata in large chunks,
  1234. * we wait until bio submission time. All the pages in the bio are
  1235. * checksummed and sums are attached onto the ordered extent record.
  1236. *
  1237. * At IO completion time the cums attached on the ordered extent record
  1238. * are inserted into the btree
  1239. */
  1240. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1241. int mirror_num, unsigned long bio_flags)
  1242. {
  1243. struct btrfs_root *root = BTRFS_I(inode)->root;
  1244. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1245. }
  1246. /*
  1247. * extent_io.c submission hook. This does the right thing for csum calculation
  1248. * on write, or reading the csums from the tree before a read
  1249. */
  1250. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1251. int mirror_num, unsigned long bio_flags)
  1252. {
  1253. struct btrfs_root *root = BTRFS_I(inode)->root;
  1254. int ret = 0;
  1255. int skip_sum;
  1256. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1257. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1258. BUG_ON(ret);
  1259. if (!(rw & (1 << BIO_RW))) {
  1260. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1261. return btrfs_submit_compressed_read(inode, bio,
  1262. mirror_num, bio_flags);
  1263. } else if (!skip_sum)
  1264. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1265. goto mapit;
  1266. } else if (!skip_sum) {
  1267. /* csum items have already been cloned */
  1268. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1269. goto mapit;
  1270. /* we're doing a write, do the async checksumming */
  1271. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1272. inode, rw, bio, mirror_num,
  1273. bio_flags, __btrfs_submit_bio_start,
  1274. __btrfs_submit_bio_done);
  1275. }
  1276. mapit:
  1277. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1278. }
  1279. /*
  1280. * given a list of ordered sums record them in the inode. This happens
  1281. * at IO completion time based on sums calculated at bio submission time.
  1282. */
  1283. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1284. struct inode *inode, u64 file_offset,
  1285. struct list_head *list)
  1286. {
  1287. struct btrfs_ordered_sum *sum;
  1288. btrfs_set_trans_block_group(trans, inode);
  1289. list_for_each_entry(sum, list, list) {
  1290. btrfs_csum_file_blocks(trans,
  1291. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1292. }
  1293. return 0;
  1294. }
  1295. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1296. struct extent_state **cached_state)
  1297. {
  1298. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1299. WARN_ON(1);
  1300. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1301. cached_state, GFP_NOFS);
  1302. }
  1303. /* see btrfs_writepage_start_hook for details on why this is required */
  1304. struct btrfs_writepage_fixup {
  1305. struct page *page;
  1306. struct btrfs_work work;
  1307. };
  1308. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1309. {
  1310. struct btrfs_writepage_fixup *fixup;
  1311. struct btrfs_ordered_extent *ordered;
  1312. struct extent_state *cached_state = NULL;
  1313. struct page *page;
  1314. struct inode *inode;
  1315. u64 page_start;
  1316. u64 page_end;
  1317. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1318. page = fixup->page;
  1319. again:
  1320. lock_page(page);
  1321. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1322. ClearPageChecked(page);
  1323. goto out_page;
  1324. }
  1325. inode = page->mapping->host;
  1326. page_start = page_offset(page);
  1327. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1328. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
  1329. &cached_state, GFP_NOFS);
  1330. /* already ordered? We're done */
  1331. if (PagePrivate2(page))
  1332. goto out;
  1333. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1334. if (ordered) {
  1335. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1336. page_end, &cached_state, GFP_NOFS);
  1337. unlock_page(page);
  1338. btrfs_start_ordered_extent(inode, ordered, 1);
  1339. goto again;
  1340. }
  1341. BUG();
  1342. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
  1343. ClearPageChecked(page);
  1344. out:
  1345. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1346. &cached_state, GFP_NOFS);
  1347. out_page:
  1348. unlock_page(page);
  1349. page_cache_release(page);
  1350. }
  1351. /*
  1352. * There are a few paths in the higher layers of the kernel that directly
  1353. * set the page dirty bit without asking the filesystem if it is a
  1354. * good idea. This causes problems because we want to make sure COW
  1355. * properly happens and the data=ordered rules are followed.
  1356. *
  1357. * In our case any range that doesn't have the ORDERED bit set
  1358. * hasn't been properly setup for IO. We kick off an async process
  1359. * to fix it up. The async helper will wait for ordered extents, set
  1360. * the delalloc bit and make it safe to write the page.
  1361. */
  1362. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1363. {
  1364. struct inode *inode = page->mapping->host;
  1365. struct btrfs_writepage_fixup *fixup;
  1366. struct btrfs_root *root = BTRFS_I(inode)->root;
  1367. /* this page is properly in the ordered list */
  1368. if (TestClearPagePrivate2(page))
  1369. return 0;
  1370. if (PageChecked(page))
  1371. return -EAGAIN;
  1372. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1373. if (!fixup)
  1374. return -EAGAIN;
  1375. SetPageChecked(page);
  1376. page_cache_get(page);
  1377. fixup->work.func = btrfs_writepage_fixup_worker;
  1378. fixup->page = page;
  1379. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1380. return -EAGAIN;
  1381. }
  1382. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1383. struct inode *inode, u64 file_pos,
  1384. u64 disk_bytenr, u64 disk_num_bytes,
  1385. u64 num_bytes, u64 ram_bytes,
  1386. u8 compression, u8 encryption,
  1387. u16 other_encoding, int extent_type)
  1388. {
  1389. struct btrfs_root *root = BTRFS_I(inode)->root;
  1390. struct btrfs_file_extent_item *fi;
  1391. struct btrfs_path *path;
  1392. struct extent_buffer *leaf;
  1393. struct btrfs_key ins;
  1394. u64 hint;
  1395. int ret;
  1396. path = btrfs_alloc_path();
  1397. BUG_ON(!path);
  1398. path->leave_spinning = 1;
  1399. /*
  1400. * we may be replacing one extent in the tree with another.
  1401. * The new extent is pinned in the extent map, and we don't want
  1402. * to drop it from the cache until it is completely in the btree.
  1403. *
  1404. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1405. * the caller is expected to unpin it and allow it to be merged
  1406. * with the others.
  1407. */
  1408. ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
  1409. &hint, 0);
  1410. BUG_ON(ret);
  1411. ins.objectid = inode->i_ino;
  1412. ins.offset = file_pos;
  1413. ins.type = BTRFS_EXTENT_DATA_KEY;
  1414. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1415. BUG_ON(ret);
  1416. leaf = path->nodes[0];
  1417. fi = btrfs_item_ptr(leaf, path->slots[0],
  1418. struct btrfs_file_extent_item);
  1419. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1420. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1421. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1422. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1423. btrfs_set_file_extent_offset(leaf, fi, 0);
  1424. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1425. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1426. btrfs_set_file_extent_compression(leaf, fi, compression);
  1427. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1428. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1429. btrfs_unlock_up_safe(path, 1);
  1430. btrfs_set_lock_blocking(leaf);
  1431. btrfs_mark_buffer_dirty(leaf);
  1432. inode_add_bytes(inode, num_bytes);
  1433. ins.objectid = disk_bytenr;
  1434. ins.offset = disk_num_bytes;
  1435. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1436. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1437. root->root_key.objectid,
  1438. inode->i_ino, file_pos, &ins);
  1439. BUG_ON(ret);
  1440. btrfs_free_path(path);
  1441. return 0;
  1442. }
  1443. /*
  1444. * helper function for btrfs_finish_ordered_io, this
  1445. * just reads in some of the csum leaves to prime them into ram
  1446. * before we start the transaction. It limits the amount of btree
  1447. * reads required while inside the transaction.
  1448. */
  1449. /* as ordered data IO finishes, this gets called so we can finish
  1450. * an ordered extent if the range of bytes in the file it covers are
  1451. * fully written.
  1452. */
  1453. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1454. {
  1455. struct btrfs_root *root = BTRFS_I(inode)->root;
  1456. struct btrfs_trans_handle *trans = NULL;
  1457. struct btrfs_ordered_extent *ordered_extent = NULL;
  1458. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1459. struct extent_state *cached_state = NULL;
  1460. int compressed = 0;
  1461. int ret;
  1462. ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  1463. end - start + 1);
  1464. if (!ret)
  1465. return 0;
  1466. BUG_ON(!ordered_extent);
  1467. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  1468. BUG_ON(!list_empty(&ordered_extent->list));
  1469. ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1470. if (!ret) {
  1471. trans = btrfs_join_transaction(root, 1);
  1472. btrfs_set_trans_block_group(trans, inode);
  1473. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1474. ret = btrfs_update_inode(trans, root, inode);
  1475. BUG_ON(ret);
  1476. }
  1477. goto out;
  1478. }
  1479. lock_extent_bits(io_tree, ordered_extent->file_offset,
  1480. ordered_extent->file_offset + ordered_extent->len - 1,
  1481. 0, &cached_state, GFP_NOFS);
  1482. trans = btrfs_join_transaction(root, 1);
  1483. btrfs_set_trans_block_group(trans, inode);
  1484. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1485. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1486. compressed = 1;
  1487. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1488. BUG_ON(compressed);
  1489. ret = btrfs_mark_extent_written(trans, inode,
  1490. ordered_extent->file_offset,
  1491. ordered_extent->file_offset +
  1492. ordered_extent->len);
  1493. BUG_ON(ret);
  1494. } else {
  1495. ret = insert_reserved_file_extent(trans, inode,
  1496. ordered_extent->file_offset,
  1497. ordered_extent->start,
  1498. ordered_extent->disk_len,
  1499. ordered_extent->len,
  1500. ordered_extent->len,
  1501. compressed, 0, 0,
  1502. BTRFS_FILE_EXTENT_REG);
  1503. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1504. ordered_extent->file_offset,
  1505. ordered_extent->len);
  1506. BUG_ON(ret);
  1507. }
  1508. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  1509. ordered_extent->file_offset +
  1510. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  1511. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1512. &ordered_extent->list);
  1513. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1514. ret = btrfs_update_inode(trans, root, inode);
  1515. BUG_ON(ret);
  1516. out:
  1517. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  1518. if (trans)
  1519. btrfs_end_transaction(trans, root);
  1520. /* once for us */
  1521. btrfs_put_ordered_extent(ordered_extent);
  1522. /* once for the tree */
  1523. btrfs_put_ordered_extent(ordered_extent);
  1524. return 0;
  1525. }
  1526. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1527. struct extent_state *state, int uptodate)
  1528. {
  1529. ClearPagePrivate2(page);
  1530. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1531. }
  1532. /*
  1533. * When IO fails, either with EIO or csum verification fails, we
  1534. * try other mirrors that might have a good copy of the data. This
  1535. * io_failure_record is used to record state as we go through all the
  1536. * mirrors. If another mirror has good data, the page is set up to date
  1537. * and things continue. If a good mirror can't be found, the original
  1538. * bio end_io callback is called to indicate things have failed.
  1539. */
  1540. struct io_failure_record {
  1541. struct page *page;
  1542. u64 start;
  1543. u64 len;
  1544. u64 logical;
  1545. unsigned long bio_flags;
  1546. int last_mirror;
  1547. };
  1548. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1549. struct page *page, u64 start, u64 end,
  1550. struct extent_state *state)
  1551. {
  1552. struct io_failure_record *failrec = NULL;
  1553. u64 private;
  1554. struct extent_map *em;
  1555. struct inode *inode = page->mapping->host;
  1556. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1557. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1558. struct bio *bio;
  1559. int num_copies;
  1560. int ret;
  1561. int rw;
  1562. u64 logical;
  1563. ret = get_state_private(failure_tree, start, &private);
  1564. if (ret) {
  1565. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1566. if (!failrec)
  1567. return -ENOMEM;
  1568. failrec->start = start;
  1569. failrec->len = end - start + 1;
  1570. failrec->last_mirror = 0;
  1571. failrec->bio_flags = 0;
  1572. read_lock(&em_tree->lock);
  1573. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1574. if (em->start > start || em->start + em->len < start) {
  1575. free_extent_map(em);
  1576. em = NULL;
  1577. }
  1578. read_unlock(&em_tree->lock);
  1579. if (!em || IS_ERR(em)) {
  1580. kfree(failrec);
  1581. return -EIO;
  1582. }
  1583. logical = start - em->start;
  1584. logical = em->block_start + logical;
  1585. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1586. logical = em->block_start;
  1587. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1588. }
  1589. failrec->logical = logical;
  1590. free_extent_map(em);
  1591. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1592. EXTENT_DIRTY, GFP_NOFS);
  1593. set_state_private(failure_tree, start,
  1594. (u64)(unsigned long)failrec);
  1595. } else {
  1596. failrec = (struct io_failure_record *)(unsigned long)private;
  1597. }
  1598. num_copies = btrfs_num_copies(
  1599. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1600. failrec->logical, failrec->len);
  1601. failrec->last_mirror++;
  1602. if (!state) {
  1603. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1604. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1605. failrec->start,
  1606. EXTENT_LOCKED);
  1607. if (state && state->start != failrec->start)
  1608. state = NULL;
  1609. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1610. }
  1611. if (!state || failrec->last_mirror > num_copies) {
  1612. set_state_private(failure_tree, failrec->start, 0);
  1613. clear_extent_bits(failure_tree, failrec->start,
  1614. failrec->start + failrec->len - 1,
  1615. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1616. kfree(failrec);
  1617. return -EIO;
  1618. }
  1619. bio = bio_alloc(GFP_NOFS, 1);
  1620. bio->bi_private = state;
  1621. bio->bi_end_io = failed_bio->bi_end_io;
  1622. bio->bi_sector = failrec->logical >> 9;
  1623. bio->bi_bdev = failed_bio->bi_bdev;
  1624. bio->bi_size = 0;
  1625. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1626. if (failed_bio->bi_rw & (1 << BIO_RW))
  1627. rw = WRITE;
  1628. else
  1629. rw = READ;
  1630. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1631. failrec->last_mirror,
  1632. failrec->bio_flags);
  1633. return 0;
  1634. }
  1635. /*
  1636. * each time an IO finishes, we do a fast check in the IO failure tree
  1637. * to see if we need to process or clean up an io_failure_record
  1638. */
  1639. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1640. {
  1641. u64 private;
  1642. u64 private_failure;
  1643. struct io_failure_record *failure;
  1644. int ret;
  1645. private = 0;
  1646. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1647. (u64)-1, 1, EXTENT_DIRTY)) {
  1648. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1649. start, &private_failure);
  1650. if (ret == 0) {
  1651. failure = (struct io_failure_record *)(unsigned long)
  1652. private_failure;
  1653. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1654. failure->start, 0);
  1655. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1656. failure->start,
  1657. failure->start + failure->len - 1,
  1658. EXTENT_DIRTY | EXTENT_LOCKED,
  1659. GFP_NOFS);
  1660. kfree(failure);
  1661. }
  1662. }
  1663. return 0;
  1664. }
  1665. /*
  1666. * when reads are done, we need to check csums to verify the data is correct
  1667. * if there's a match, we allow the bio to finish. If not, we go through
  1668. * the io_failure_record routines to find good copies
  1669. */
  1670. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1671. struct extent_state *state)
  1672. {
  1673. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1674. struct inode *inode = page->mapping->host;
  1675. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1676. char *kaddr;
  1677. u64 private = ~(u32)0;
  1678. int ret;
  1679. struct btrfs_root *root = BTRFS_I(inode)->root;
  1680. u32 csum = ~(u32)0;
  1681. if (PageChecked(page)) {
  1682. ClearPageChecked(page);
  1683. goto good;
  1684. }
  1685. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1686. return 0;
  1687. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1688. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1689. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1690. GFP_NOFS);
  1691. return 0;
  1692. }
  1693. if (state && state->start == start) {
  1694. private = state->private;
  1695. ret = 0;
  1696. } else {
  1697. ret = get_state_private(io_tree, start, &private);
  1698. }
  1699. kaddr = kmap_atomic(page, KM_USER0);
  1700. if (ret)
  1701. goto zeroit;
  1702. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1703. btrfs_csum_final(csum, (char *)&csum);
  1704. if (csum != private)
  1705. goto zeroit;
  1706. kunmap_atomic(kaddr, KM_USER0);
  1707. good:
  1708. /* if the io failure tree for this inode is non-empty,
  1709. * check to see if we've recovered from a failed IO
  1710. */
  1711. btrfs_clean_io_failures(inode, start);
  1712. return 0;
  1713. zeroit:
  1714. if (printk_ratelimit()) {
  1715. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1716. "private %llu\n", page->mapping->host->i_ino,
  1717. (unsigned long long)start, csum,
  1718. (unsigned long long)private);
  1719. }
  1720. memset(kaddr + offset, 1, end - start + 1);
  1721. flush_dcache_page(page);
  1722. kunmap_atomic(kaddr, KM_USER0);
  1723. if (private == 0)
  1724. return 0;
  1725. return -EIO;
  1726. }
  1727. struct delayed_iput {
  1728. struct list_head list;
  1729. struct inode *inode;
  1730. };
  1731. void btrfs_add_delayed_iput(struct inode *inode)
  1732. {
  1733. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1734. struct delayed_iput *delayed;
  1735. if (atomic_add_unless(&inode->i_count, -1, 1))
  1736. return;
  1737. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  1738. delayed->inode = inode;
  1739. spin_lock(&fs_info->delayed_iput_lock);
  1740. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  1741. spin_unlock(&fs_info->delayed_iput_lock);
  1742. }
  1743. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  1744. {
  1745. LIST_HEAD(list);
  1746. struct btrfs_fs_info *fs_info = root->fs_info;
  1747. struct delayed_iput *delayed;
  1748. int empty;
  1749. spin_lock(&fs_info->delayed_iput_lock);
  1750. empty = list_empty(&fs_info->delayed_iputs);
  1751. spin_unlock(&fs_info->delayed_iput_lock);
  1752. if (empty)
  1753. return;
  1754. down_read(&root->fs_info->cleanup_work_sem);
  1755. spin_lock(&fs_info->delayed_iput_lock);
  1756. list_splice_init(&fs_info->delayed_iputs, &list);
  1757. spin_unlock(&fs_info->delayed_iput_lock);
  1758. while (!list_empty(&list)) {
  1759. delayed = list_entry(list.next, struct delayed_iput, list);
  1760. list_del(&delayed->list);
  1761. iput(delayed->inode);
  1762. kfree(delayed);
  1763. }
  1764. up_read(&root->fs_info->cleanup_work_sem);
  1765. }
  1766. /*
  1767. * calculate extra metadata reservation when snapshotting a subvolume
  1768. * contains orphan files.
  1769. */
  1770. void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
  1771. struct btrfs_pending_snapshot *pending,
  1772. u64 *bytes_to_reserve)
  1773. {
  1774. struct btrfs_root *root;
  1775. struct btrfs_block_rsv *block_rsv;
  1776. u64 num_bytes;
  1777. int index;
  1778. root = pending->root;
  1779. if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
  1780. return;
  1781. block_rsv = root->orphan_block_rsv;
  1782. /* orphan block reservation for the snapshot */
  1783. num_bytes = block_rsv->size;
  1784. /*
  1785. * after the snapshot is created, COWing tree blocks may use more
  1786. * space than it frees. So we should make sure there is enough
  1787. * reserved space.
  1788. */
  1789. index = trans->transid & 0x1;
  1790. if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
  1791. num_bytes += block_rsv->size -
  1792. (block_rsv->reserved + block_rsv->freed[index]);
  1793. }
  1794. *bytes_to_reserve += num_bytes;
  1795. }
  1796. void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
  1797. struct btrfs_pending_snapshot *pending)
  1798. {
  1799. struct btrfs_root *root = pending->root;
  1800. struct btrfs_root *snap = pending->snap;
  1801. struct btrfs_block_rsv *block_rsv;
  1802. u64 num_bytes;
  1803. int index;
  1804. int ret;
  1805. if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
  1806. return;
  1807. /* refill source subvolume's orphan block reservation */
  1808. block_rsv = root->orphan_block_rsv;
  1809. index = trans->transid & 0x1;
  1810. if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
  1811. num_bytes = block_rsv->size -
  1812. (block_rsv->reserved + block_rsv->freed[index]);
  1813. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  1814. root->orphan_block_rsv,
  1815. num_bytes);
  1816. BUG_ON(ret);
  1817. }
  1818. /* setup orphan block reservation for the snapshot */
  1819. block_rsv = btrfs_alloc_block_rsv(snap);
  1820. BUG_ON(!block_rsv);
  1821. btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
  1822. snap->orphan_block_rsv = block_rsv;
  1823. num_bytes = root->orphan_block_rsv->size;
  1824. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  1825. block_rsv, num_bytes);
  1826. BUG_ON(ret);
  1827. #if 0
  1828. /* insert orphan item for the snapshot */
  1829. WARN_ON(!root->orphan_item_inserted);
  1830. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1831. snap->root_key.objectid);
  1832. BUG_ON(ret);
  1833. snap->orphan_item_inserted = 1;
  1834. #endif
  1835. }
  1836. enum btrfs_orphan_cleanup_state {
  1837. ORPHAN_CLEANUP_STARTED = 1,
  1838. ORPHAN_CLEANUP_DONE = 2,
  1839. };
  1840. /*
  1841. * This is called in transaction commmit time. If there are no orphan
  1842. * files in the subvolume, it removes orphan item and frees block_rsv
  1843. * structure.
  1844. */
  1845. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  1846. struct btrfs_root *root)
  1847. {
  1848. int ret;
  1849. if (!list_empty(&root->orphan_list) ||
  1850. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  1851. return;
  1852. if (root->orphan_item_inserted &&
  1853. btrfs_root_refs(&root->root_item) > 0) {
  1854. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  1855. root->root_key.objectid);
  1856. BUG_ON(ret);
  1857. root->orphan_item_inserted = 0;
  1858. }
  1859. if (root->orphan_block_rsv) {
  1860. WARN_ON(root->orphan_block_rsv->size > 0);
  1861. btrfs_free_block_rsv(root, root->orphan_block_rsv);
  1862. root->orphan_block_rsv = NULL;
  1863. }
  1864. }
  1865. /*
  1866. * This creates an orphan entry for the given inode in case something goes
  1867. * wrong in the middle of an unlink/truncate.
  1868. *
  1869. * NOTE: caller of this function should reserve 5 units of metadata for
  1870. * this function.
  1871. */
  1872. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1873. {
  1874. struct btrfs_root *root = BTRFS_I(inode)->root;
  1875. struct btrfs_block_rsv *block_rsv = NULL;
  1876. int reserve = 0;
  1877. int insert = 0;
  1878. int ret;
  1879. if (!root->orphan_block_rsv) {
  1880. block_rsv = btrfs_alloc_block_rsv(root);
  1881. BUG_ON(!block_rsv);
  1882. }
  1883. spin_lock(&root->orphan_lock);
  1884. if (!root->orphan_block_rsv) {
  1885. root->orphan_block_rsv = block_rsv;
  1886. } else if (block_rsv) {
  1887. btrfs_free_block_rsv(root, block_rsv);
  1888. block_rsv = NULL;
  1889. }
  1890. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1891. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1892. #if 0
  1893. /*
  1894. * For proper ENOSPC handling, we should do orphan
  1895. * cleanup when mounting. But this introduces backward
  1896. * compatibility issue.
  1897. */
  1898. if (!xchg(&root->orphan_item_inserted, 1))
  1899. insert = 2;
  1900. else
  1901. insert = 1;
  1902. #endif
  1903. insert = 1;
  1904. } else {
  1905. WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
  1906. }
  1907. if (!BTRFS_I(inode)->orphan_meta_reserved) {
  1908. BTRFS_I(inode)->orphan_meta_reserved = 1;
  1909. reserve = 1;
  1910. }
  1911. spin_unlock(&root->orphan_lock);
  1912. if (block_rsv)
  1913. btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
  1914. /* grab metadata reservation from transaction handle */
  1915. if (reserve) {
  1916. ret = btrfs_orphan_reserve_metadata(trans, inode);
  1917. BUG_ON(ret);
  1918. }
  1919. /* insert an orphan item to track this unlinked/truncated file */
  1920. if (insert >= 1) {
  1921. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1922. BUG_ON(ret);
  1923. }
  1924. /* insert an orphan item to track subvolume contains orphan files */
  1925. if (insert >= 2) {
  1926. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1927. root->root_key.objectid);
  1928. BUG_ON(ret);
  1929. }
  1930. return 0;
  1931. }
  1932. /*
  1933. * We have done the truncate/delete so we can go ahead and remove the orphan
  1934. * item for this particular inode.
  1935. */
  1936. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1937. {
  1938. struct btrfs_root *root = BTRFS_I(inode)->root;
  1939. int delete_item = 0;
  1940. int release_rsv = 0;
  1941. int ret = 0;
  1942. spin_lock(&root->orphan_lock);
  1943. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1944. list_del_init(&BTRFS_I(inode)->i_orphan);
  1945. delete_item = 1;
  1946. }
  1947. if (BTRFS_I(inode)->orphan_meta_reserved) {
  1948. BTRFS_I(inode)->orphan_meta_reserved = 0;
  1949. release_rsv = 1;
  1950. }
  1951. spin_unlock(&root->orphan_lock);
  1952. if (trans && delete_item) {
  1953. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1954. BUG_ON(ret);
  1955. }
  1956. if (release_rsv)
  1957. btrfs_orphan_release_metadata(inode);
  1958. return 0;
  1959. }
  1960. /*
  1961. * this cleans up any orphans that may be left on the list from the last use
  1962. * of this root.
  1963. */
  1964. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1965. {
  1966. struct btrfs_path *path;
  1967. struct extent_buffer *leaf;
  1968. struct btrfs_item *item;
  1969. struct btrfs_key key, found_key;
  1970. struct btrfs_trans_handle *trans;
  1971. struct inode *inode;
  1972. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1973. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  1974. return;
  1975. path = btrfs_alloc_path();
  1976. BUG_ON(!path);
  1977. path->reada = -1;
  1978. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1979. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1980. key.offset = (u64)-1;
  1981. while (1) {
  1982. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1983. if (ret < 0) {
  1984. printk(KERN_ERR "Error searching slot for orphan: %d"
  1985. "\n", ret);
  1986. break;
  1987. }
  1988. /*
  1989. * if ret == 0 means we found what we were searching for, which
  1990. * is weird, but possible, so only screw with path if we didnt
  1991. * find the key and see if we have stuff that matches
  1992. */
  1993. if (ret > 0) {
  1994. if (path->slots[0] == 0)
  1995. break;
  1996. path->slots[0]--;
  1997. }
  1998. /* pull out the item */
  1999. leaf = path->nodes[0];
  2000. item = btrfs_item_nr(leaf, path->slots[0]);
  2001. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2002. /* make sure the item matches what we want */
  2003. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2004. break;
  2005. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2006. break;
  2007. /* release the path since we're done with it */
  2008. btrfs_release_path(root, path);
  2009. /*
  2010. * this is where we are basically btrfs_lookup, without the
  2011. * crossing root thing. we store the inode number in the
  2012. * offset of the orphan item.
  2013. */
  2014. found_key.objectid = found_key.offset;
  2015. found_key.type = BTRFS_INODE_ITEM_KEY;
  2016. found_key.offset = 0;
  2017. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2018. BUG_ON(IS_ERR(inode));
  2019. /*
  2020. * add this inode to the orphan list so btrfs_orphan_del does
  2021. * the proper thing when we hit it
  2022. */
  2023. spin_lock(&root->orphan_lock);
  2024. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  2025. spin_unlock(&root->orphan_lock);
  2026. /*
  2027. * if this is a bad inode, means we actually succeeded in
  2028. * removing the inode, but not the orphan record, which means
  2029. * we need to manually delete the orphan since iput will just
  2030. * do a destroy_inode
  2031. */
  2032. if (is_bad_inode(inode)) {
  2033. trans = btrfs_start_transaction(root, 0);
  2034. btrfs_orphan_del(trans, inode);
  2035. btrfs_end_transaction(trans, root);
  2036. iput(inode);
  2037. continue;
  2038. }
  2039. /* if we have links, this was a truncate, lets do that */
  2040. if (inode->i_nlink) {
  2041. nr_truncate++;
  2042. btrfs_truncate(inode);
  2043. } else {
  2044. nr_unlink++;
  2045. }
  2046. /* this will do delete_inode and everything for us */
  2047. iput(inode);
  2048. }
  2049. btrfs_free_path(path);
  2050. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2051. if (root->orphan_block_rsv)
  2052. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2053. (u64)-1);
  2054. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2055. trans = btrfs_join_transaction(root, 1);
  2056. btrfs_end_transaction(trans, root);
  2057. }
  2058. if (nr_unlink)
  2059. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  2060. if (nr_truncate)
  2061. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  2062. }
  2063. /*
  2064. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2065. * don't find any xattrs, we know there can't be any acls.
  2066. *
  2067. * slot is the slot the inode is in, objectid is the objectid of the inode
  2068. */
  2069. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2070. int slot, u64 objectid)
  2071. {
  2072. u32 nritems = btrfs_header_nritems(leaf);
  2073. struct btrfs_key found_key;
  2074. int scanned = 0;
  2075. slot++;
  2076. while (slot < nritems) {
  2077. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2078. /* we found a different objectid, there must not be acls */
  2079. if (found_key.objectid != objectid)
  2080. return 0;
  2081. /* we found an xattr, assume we've got an acl */
  2082. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2083. return 1;
  2084. /*
  2085. * we found a key greater than an xattr key, there can't
  2086. * be any acls later on
  2087. */
  2088. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2089. return 0;
  2090. slot++;
  2091. scanned++;
  2092. /*
  2093. * it goes inode, inode backrefs, xattrs, extents,
  2094. * so if there are a ton of hard links to an inode there can
  2095. * be a lot of backrefs. Don't waste time searching too hard,
  2096. * this is just an optimization
  2097. */
  2098. if (scanned >= 8)
  2099. break;
  2100. }
  2101. /* we hit the end of the leaf before we found an xattr or
  2102. * something larger than an xattr. We have to assume the inode
  2103. * has acls
  2104. */
  2105. return 1;
  2106. }
  2107. /*
  2108. * read an inode from the btree into the in-memory inode
  2109. */
  2110. static void btrfs_read_locked_inode(struct inode *inode)
  2111. {
  2112. struct btrfs_path *path;
  2113. struct extent_buffer *leaf;
  2114. struct btrfs_inode_item *inode_item;
  2115. struct btrfs_timespec *tspec;
  2116. struct btrfs_root *root = BTRFS_I(inode)->root;
  2117. struct btrfs_key location;
  2118. int maybe_acls;
  2119. u64 alloc_group_block;
  2120. u32 rdev;
  2121. int ret;
  2122. path = btrfs_alloc_path();
  2123. BUG_ON(!path);
  2124. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2125. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2126. if (ret)
  2127. goto make_bad;
  2128. leaf = path->nodes[0];
  2129. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2130. struct btrfs_inode_item);
  2131. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2132. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  2133. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  2134. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  2135. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2136. tspec = btrfs_inode_atime(inode_item);
  2137. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2138. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2139. tspec = btrfs_inode_mtime(inode_item);
  2140. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2141. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2142. tspec = btrfs_inode_ctime(inode_item);
  2143. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2144. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2145. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2146. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2147. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  2148. inode->i_generation = BTRFS_I(inode)->generation;
  2149. inode->i_rdev = 0;
  2150. rdev = btrfs_inode_rdev(leaf, inode_item);
  2151. BTRFS_I(inode)->index_cnt = (u64)-1;
  2152. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2153. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  2154. /*
  2155. * try to precache a NULL acl entry for files that don't have
  2156. * any xattrs or acls
  2157. */
  2158. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  2159. if (!maybe_acls)
  2160. cache_no_acl(inode);
  2161. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  2162. alloc_group_block, 0);
  2163. btrfs_free_path(path);
  2164. inode_item = NULL;
  2165. switch (inode->i_mode & S_IFMT) {
  2166. case S_IFREG:
  2167. inode->i_mapping->a_ops = &btrfs_aops;
  2168. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2169. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2170. inode->i_fop = &btrfs_file_operations;
  2171. inode->i_op = &btrfs_file_inode_operations;
  2172. break;
  2173. case S_IFDIR:
  2174. inode->i_fop = &btrfs_dir_file_operations;
  2175. if (root == root->fs_info->tree_root)
  2176. inode->i_op = &btrfs_dir_ro_inode_operations;
  2177. else
  2178. inode->i_op = &btrfs_dir_inode_operations;
  2179. break;
  2180. case S_IFLNK:
  2181. inode->i_op = &btrfs_symlink_inode_operations;
  2182. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2183. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2184. break;
  2185. default:
  2186. inode->i_op = &btrfs_special_inode_operations;
  2187. init_special_inode(inode, inode->i_mode, rdev);
  2188. break;
  2189. }
  2190. btrfs_update_iflags(inode);
  2191. return;
  2192. make_bad:
  2193. btrfs_free_path(path);
  2194. make_bad_inode(inode);
  2195. }
  2196. /*
  2197. * given a leaf and an inode, copy the inode fields into the leaf
  2198. */
  2199. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2200. struct extent_buffer *leaf,
  2201. struct btrfs_inode_item *item,
  2202. struct inode *inode)
  2203. {
  2204. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2205. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2206. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2207. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2208. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2209. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2210. inode->i_atime.tv_sec);
  2211. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2212. inode->i_atime.tv_nsec);
  2213. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2214. inode->i_mtime.tv_sec);
  2215. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2216. inode->i_mtime.tv_nsec);
  2217. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2218. inode->i_ctime.tv_sec);
  2219. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2220. inode->i_ctime.tv_nsec);
  2221. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2222. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2223. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2224. btrfs_set_inode_transid(leaf, item, trans->transid);
  2225. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2226. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2227. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2228. }
  2229. /*
  2230. * copy everything in the in-memory inode into the btree.
  2231. */
  2232. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2233. struct btrfs_root *root, struct inode *inode)
  2234. {
  2235. struct btrfs_inode_item *inode_item;
  2236. struct btrfs_path *path;
  2237. struct extent_buffer *leaf;
  2238. int ret;
  2239. path = btrfs_alloc_path();
  2240. BUG_ON(!path);
  2241. path->leave_spinning = 1;
  2242. ret = btrfs_lookup_inode(trans, root, path,
  2243. &BTRFS_I(inode)->location, 1);
  2244. if (ret) {
  2245. if (ret > 0)
  2246. ret = -ENOENT;
  2247. goto failed;
  2248. }
  2249. btrfs_unlock_up_safe(path, 1);
  2250. leaf = path->nodes[0];
  2251. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2252. struct btrfs_inode_item);
  2253. fill_inode_item(trans, leaf, inode_item, inode);
  2254. btrfs_mark_buffer_dirty(leaf);
  2255. btrfs_set_inode_last_trans(trans, inode);
  2256. ret = 0;
  2257. failed:
  2258. btrfs_free_path(path);
  2259. return ret;
  2260. }
  2261. /*
  2262. * unlink helper that gets used here in inode.c and in the tree logging
  2263. * recovery code. It remove a link in a directory with a given name, and
  2264. * also drops the back refs in the inode to the directory
  2265. */
  2266. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2267. struct btrfs_root *root,
  2268. struct inode *dir, struct inode *inode,
  2269. const char *name, int name_len)
  2270. {
  2271. struct btrfs_path *path;
  2272. int ret = 0;
  2273. struct extent_buffer *leaf;
  2274. struct btrfs_dir_item *di;
  2275. struct btrfs_key key;
  2276. u64 index;
  2277. path = btrfs_alloc_path();
  2278. if (!path) {
  2279. ret = -ENOMEM;
  2280. goto err;
  2281. }
  2282. path->leave_spinning = 1;
  2283. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2284. name, name_len, -1);
  2285. if (IS_ERR(di)) {
  2286. ret = PTR_ERR(di);
  2287. goto err;
  2288. }
  2289. if (!di) {
  2290. ret = -ENOENT;
  2291. goto err;
  2292. }
  2293. leaf = path->nodes[0];
  2294. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2295. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2296. if (ret)
  2297. goto err;
  2298. btrfs_release_path(root, path);
  2299. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2300. inode->i_ino,
  2301. dir->i_ino, &index);
  2302. if (ret) {
  2303. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2304. "inode %lu parent %lu\n", name_len, name,
  2305. inode->i_ino, dir->i_ino);
  2306. goto err;
  2307. }
  2308. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2309. index, name, name_len, -1);
  2310. if (IS_ERR(di)) {
  2311. ret = PTR_ERR(di);
  2312. goto err;
  2313. }
  2314. if (!di) {
  2315. ret = -ENOENT;
  2316. goto err;
  2317. }
  2318. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2319. btrfs_release_path(root, path);
  2320. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2321. inode, dir->i_ino);
  2322. BUG_ON(ret != 0 && ret != -ENOENT);
  2323. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2324. dir, index);
  2325. BUG_ON(ret);
  2326. err:
  2327. btrfs_free_path(path);
  2328. if (ret)
  2329. goto out;
  2330. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2331. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2332. btrfs_update_inode(trans, root, dir);
  2333. btrfs_drop_nlink(inode);
  2334. ret = btrfs_update_inode(trans, root, inode);
  2335. out:
  2336. return ret;
  2337. }
  2338. /* helper to check if there is any shared block in the path */
  2339. static int check_path_shared(struct btrfs_root *root,
  2340. struct btrfs_path *path)
  2341. {
  2342. struct extent_buffer *eb;
  2343. int level;
  2344. int ret;
  2345. u64 refs;
  2346. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2347. if (!path->nodes[level])
  2348. break;
  2349. eb = path->nodes[level];
  2350. if (!btrfs_block_can_be_shared(root, eb))
  2351. continue;
  2352. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  2353. &refs, NULL);
  2354. if (refs > 1)
  2355. return 1;
  2356. }
  2357. return 0;
  2358. }
  2359. /*
  2360. * helper to start transaction for unlink and rmdir.
  2361. *
  2362. * unlink and rmdir are special in btrfs, they do not always free space.
  2363. * so in enospc case, we should make sure they will free space before
  2364. * allowing them to use the global metadata reservation.
  2365. */
  2366. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  2367. struct dentry *dentry)
  2368. {
  2369. struct btrfs_trans_handle *trans;
  2370. struct btrfs_root *root = BTRFS_I(dir)->root;
  2371. struct btrfs_path *path;
  2372. struct btrfs_inode_ref *ref;
  2373. struct btrfs_dir_item *di;
  2374. struct inode *inode = dentry->d_inode;
  2375. u64 index;
  2376. int check_link = 1;
  2377. int err = -ENOSPC;
  2378. int ret;
  2379. trans = btrfs_start_transaction(root, 10);
  2380. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  2381. return trans;
  2382. if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  2383. return ERR_PTR(-ENOSPC);
  2384. /* check if there is someone else holds reference */
  2385. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  2386. return ERR_PTR(-ENOSPC);
  2387. if (atomic_read(&inode->i_count) > 2)
  2388. return ERR_PTR(-ENOSPC);
  2389. if (xchg(&root->fs_info->enospc_unlink, 1))
  2390. return ERR_PTR(-ENOSPC);
  2391. path = btrfs_alloc_path();
  2392. if (!path) {
  2393. root->fs_info->enospc_unlink = 0;
  2394. return ERR_PTR(-ENOMEM);
  2395. }
  2396. trans = btrfs_start_transaction(root, 0);
  2397. if (IS_ERR(trans)) {
  2398. btrfs_free_path(path);
  2399. root->fs_info->enospc_unlink = 0;
  2400. return trans;
  2401. }
  2402. path->skip_locking = 1;
  2403. path->search_commit_root = 1;
  2404. ret = btrfs_lookup_inode(trans, root, path,
  2405. &BTRFS_I(dir)->location, 0);
  2406. if (ret < 0) {
  2407. err = ret;
  2408. goto out;
  2409. }
  2410. if (ret == 0) {
  2411. if (check_path_shared(root, path))
  2412. goto out;
  2413. } else {
  2414. check_link = 0;
  2415. }
  2416. btrfs_release_path(root, path);
  2417. ret = btrfs_lookup_inode(trans, root, path,
  2418. &BTRFS_I(inode)->location, 0);
  2419. if (ret < 0) {
  2420. err = ret;
  2421. goto out;
  2422. }
  2423. if (ret == 0) {
  2424. if (check_path_shared(root, path))
  2425. goto out;
  2426. } else {
  2427. check_link = 0;
  2428. }
  2429. btrfs_release_path(root, path);
  2430. if (ret == 0 && S_ISREG(inode->i_mode)) {
  2431. ret = btrfs_lookup_file_extent(trans, root, path,
  2432. inode->i_ino, (u64)-1, 0);
  2433. if (ret < 0) {
  2434. err = ret;
  2435. goto out;
  2436. }
  2437. BUG_ON(ret == 0);
  2438. if (check_path_shared(root, path))
  2439. goto out;
  2440. btrfs_release_path(root, path);
  2441. }
  2442. if (!check_link) {
  2443. err = 0;
  2444. goto out;
  2445. }
  2446. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2447. dentry->d_name.name, dentry->d_name.len, 0);
  2448. if (IS_ERR(di)) {
  2449. err = PTR_ERR(di);
  2450. goto out;
  2451. }
  2452. if (di) {
  2453. if (check_path_shared(root, path))
  2454. goto out;
  2455. } else {
  2456. err = 0;
  2457. goto out;
  2458. }
  2459. btrfs_release_path(root, path);
  2460. ref = btrfs_lookup_inode_ref(trans, root, path,
  2461. dentry->d_name.name, dentry->d_name.len,
  2462. inode->i_ino, dir->i_ino, 0);
  2463. if (IS_ERR(ref)) {
  2464. err = PTR_ERR(ref);
  2465. goto out;
  2466. }
  2467. BUG_ON(!ref);
  2468. if (check_path_shared(root, path))
  2469. goto out;
  2470. index = btrfs_inode_ref_index(path->nodes[0], ref);
  2471. btrfs_release_path(root, path);
  2472. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
  2473. dentry->d_name.name, dentry->d_name.len, 0);
  2474. if (IS_ERR(di)) {
  2475. err = PTR_ERR(di);
  2476. goto out;
  2477. }
  2478. BUG_ON(ret == -ENOENT);
  2479. if (check_path_shared(root, path))
  2480. goto out;
  2481. err = 0;
  2482. out:
  2483. btrfs_free_path(path);
  2484. if (err) {
  2485. btrfs_end_transaction(trans, root);
  2486. root->fs_info->enospc_unlink = 0;
  2487. return ERR_PTR(err);
  2488. }
  2489. trans->block_rsv = &root->fs_info->global_block_rsv;
  2490. return trans;
  2491. }
  2492. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  2493. struct btrfs_root *root)
  2494. {
  2495. if (trans->block_rsv == &root->fs_info->global_block_rsv) {
  2496. BUG_ON(!root->fs_info->enospc_unlink);
  2497. root->fs_info->enospc_unlink = 0;
  2498. }
  2499. btrfs_end_transaction_throttle(trans, root);
  2500. }
  2501. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2502. {
  2503. struct btrfs_root *root = BTRFS_I(dir)->root;
  2504. struct btrfs_trans_handle *trans;
  2505. struct inode *inode = dentry->d_inode;
  2506. int ret;
  2507. unsigned long nr = 0;
  2508. trans = __unlink_start_trans(dir, dentry);
  2509. if (IS_ERR(trans))
  2510. return PTR_ERR(trans);
  2511. btrfs_set_trans_block_group(trans, dir);
  2512. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2513. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2514. dentry->d_name.name, dentry->d_name.len);
  2515. BUG_ON(ret);
  2516. if (inode->i_nlink == 0) {
  2517. ret = btrfs_orphan_add(trans, inode);
  2518. BUG_ON(ret);
  2519. }
  2520. nr = trans->blocks_used;
  2521. __unlink_end_trans(trans, root);
  2522. btrfs_btree_balance_dirty(root, nr);
  2523. return ret;
  2524. }
  2525. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2526. struct btrfs_root *root,
  2527. struct inode *dir, u64 objectid,
  2528. const char *name, int name_len)
  2529. {
  2530. struct btrfs_path *path;
  2531. struct extent_buffer *leaf;
  2532. struct btrfs_dir_item *di;
  2533. struct btrfs_key key;
  2534. u64 index;
  2535. int ret;
  2536. path = btrfs_alloc_path();
  2537. if (!path)
  2538. return -ENOMEM;
  2539. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2540. name, name_len, -1);
  2541. BUG_ON(!di || IS_ERR(di));
  2542. leaf = path->nodes[0];
  2543. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2544. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2545. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2546. BUG_ON(ret);
  2547. btrfs_release_path(root, path);
  2548. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2549. objectid, root->root_key.objectid,
  2550. dir->i_ino, &index, name, name_len);
  2551. if (ret < 0) {
  2552. BUG_ON(ret != -ENOENT);
  2553. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2554. name, name_len);
  2555. BUG_ON(!di || IS_ERR(di));
  2556. leaf = path->nodes[0];
  2557. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2558. btrfs_release_path(root, path);
  2559. index = key.offset;
  2560. }
  2561. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2562. index, name, name_len, -1);
  2563. BUG_ON(!di || IS_ERR(di));
  2564. leaf = path->nodes[0];
  2565. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2566. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2567. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2568. BUG_ON(ret);
  2569. btrfs_release_path(root, path);
  2570. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2571. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2572. ret = btrfs_update_inode(trans, root, dir);
  2573. BUG_ON(ret);
  2574. dir->i_sb->s_dirt = 1;
  2575. btrfs_free_path(path);
  2576. return 0;
  2577. }
  2578. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2579. {
  2580. struct inode *inode = dentry->d_inode;
  2581. int err = 0;
  2582. struct btrfs_root *root = BTRFS_I(dir)->root;
  2583. struct btrfs_trans_handle *trans;
  2584. unsigned long nr = 0;
  2585. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2586. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2587. return -ENOTEMPTY;
  2588. trans = __unlink_start_trans(dir, dentry);
  2589. if (IS_ERR(trans))
  2590. return PTR_ERR(trans);
  2591. btrfs_set_trans_block_group(trans, dir);
  2592. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2593. err = btrfs_unlink_subvol(trans, root, dir,
  2594. BTRFS_I(inode)->location.objectid,
  2595. dentry->d_name.name,
  2596. dentry->d_name.len);
  2597. goto out;
  2598. }
  2599. err = btrfs_orphan_add(trans, inode);
  2600. if (err)
  2601. goto out;
  2602. /* now the directory is empty */
  2603. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2604. dentry->d_name.name, dentry->d_name.len);
  2605. if (!err)
  2606. btrfs_i_size_write(inode, 0);
  2607. out:
  2608. nr = trans->blocks_used;
  2609. __unlink_end_trans(trans, root);
  2610. btrfs_btree_balance_dirty(root, nr);
  2611. return err;
  2612. }
  2613. #if 0
  2614. /*
  2615. * when truncating bytes in a file, it is possible to avoid reading
  2616. * the leaves that contain only checksum items. This can be the
  2617. * majority of the IO required to delete a large file, but it must
  2618. * be done carefully.
  2619. *
  2620. * The keys in the level just above the leaves are checked to make sure
  2621. * the lowest key in a given leaf is a csum key, and starts at an offset
  2622. * after the new size.
  2623. *
  2624. * Then the key for the next leaf is checked to make sure it also has
  2625. * a checksum item for the same file. If it does, we know our target leaf
  2626. * contains only checksum items, and it can be safely freed without reading
  2627. * it.
  2628. *
  2629. * This is just an optimization targeted at large files. It may do
  2630. * nothing. It will return 0 unless things went badly.
  2631. */
  2632. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2633. struct btrfs_root *root,
  2634. struct btrfs_path *path,
  2635. struct inode *inode, u64 new_size)
  2636. {
  2637. struct btrfs_key key;
  2638. int ret;
  2639. int nritems;
  2640. struct btrfs_key found_key;
  2641. struct btrfs_key other_key;
  2642. struct btrfs_leaf_ref *ref;
  2643. u64 leaf_gen;
  2644. u64 leaf_start;
  2645. path->lowest_level = 1;
  2646. key.objectid = inode->i_ino;
  2647. key.type = BTRFS_CSUM_ITEM_KEY;
  2648. key.offset = new_size;
  2649. again:
  2650. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2651. if (ret < 0)
  2652. goto out;
  2653. if (path->nodes[1] == NULL) {
  2654. ret = 0;
  2655. goto out;
  2656. }
  2657. ret = 0;
  2658. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2659. nritems = btrfs_header_nritems(path->nodes[1]);
  2660. if (!nritems)
  2661. goto out;
  2662. if (path->slots[1] >= nritems)
  2663. goto next_node;
  2664. /* did we find a key greater than anything we want to delete? */
  2665. if (found_key.objectid > inode->i_ino ||
  2666. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2667. goto out;
  2668. /* we check the next key in the node to make sure the leave contains
  2669. * only checksum items. This comparison doesn't work if our
  2670. * leaf is the last one in the node
  2671. */
  2672. if (path->slots[1] + 1 >= nritems) {
  2673. next_node:
  2674. /* search forward from the last key in the node, this
  2675. * will bring us into the next node in the tree
  2676. */
  2677. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2678. /* unlikely, but we inc below, so check to be safe */
  2679. if (found_key.offset == (u64)-1)
  2680. goto out;
  2681. /* search_forward needs a path with locks held, do the
  2682. * search again for the original key. It is possible
  2683. * this will race with a balance and return a path that
  2684. * we could modify, but this drop is just an optimization
  2685. * and is allowed to miss some leaves.
  2686. */
  2687. btrfs_release_path(root, path);
  2688. found_key.offset++;
  2689. /* setup a max key for search_forward */
  2690. other_key.offset = (u64)-1;
  2691. other_key.type = key.type;
  2692. other_key.objectid = key.objectid;
  2693. path->keep_locks = 1;
  2694. ret = btrfs_search_forward(root, &found_key, &other_key,
  2695. path, 0, 0);
  2696. path->keep_locks = 0;
  2697. if (ret || found_key.objectid != key.objectid ||
  2698. found_key.type != key.type) {
  2699. ret = 0;
  2700. goto out;
  2701. }
  2702. key.offset = found_key.offset;
  2703. btrfs_release_path(root, path);
  2704. cond_resched();
  2705. goto again;
  2706. }
  2707. /* we know there's one more slot after us in the tree,
  2708. * read that key so we can verify it is also a checksum item
  2709. */
  2710. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2711. if (found_key.objectid < inode->i_ino)
  2712. goto next_key;
  2713. if (found_key.type != key.type || found_key.offset < new_size)
  2714. goto next_key;
  2715. /*
  2716. * if the key for the next leaf isn't a csum key from this objectid,
  2717. * we can't be sure there aren't good items inside this leaf.
  2718. * Bail out
  2719. */
  2720. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2721. goto out;
  2722. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2723. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2724. /*
  2725. * it is safe to delete this leaf, it contains only
  2726. * csum items from this inode at an offset >= new_size
  2727. */
  2728. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2729. BUG_ON(ret);
  2730. if (root->ref_cows && leaf_gen < trans->transid) {
  2731. ref = btrfs_alloc_leaf_ref(root, 0);
  2732. if (ref) {
  2733. ref->root_gen = root->root_key.offset;
  2734. ref->bytenr = leaf_start;
  2735. ref->owner = 0;
  2736. ref->generation = leaf_gen;
  2737. ref->nritems = 0;
  2738. btrfs_sort_leaf_ref(ref);
  2739. ret = btrfs_add_leaf_ref(root, ref, 0);
  2740. WARN_ON(ret);
  2741. btrfs_free_leaf_ref(root, ref);
  2742. } else {
  2743. WARN_ON(1);
  2744. }
  2745. }
  2746. next_key:
  2747. btrfs_release_path(root, path);
  2748. if (other_key.objectid == inode->i_ino &&
  2749. other_key.type == key.type && other_key.offset > key.offset) {
  2750. key.offset = other_key.offset;
  2751. cond_resched();
  2752. goto again;
  2753. }
  2754. ret = 0;
  2755. out:
  2756. /* fixup any changes we've made to the path */
  2757. path->lowest_level = 0;
  2758. path->keep_locks = 0;
  2759. btrfs_release_path(root, path);
  2760. return ret;
  2761. }
  2762. #endif
  2763. /*
  2764. * this can truncate away extent items, csum items and directory items.
  2765. * It starts at a high offset and removes keys until it can't find
  2766. * any higher than new_size
  2767. *
  2768. * csum items that cross the new i_size are truncated to the new size
  2769. * as well.
  2770. *
  2771. * min_type is the minimum key type to truncate down to. If set to 0, this
  2772. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2773. */
  2774. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2775. struct btrfs_root *root,
  2776. struct inode *inode,
  2777. u64 new_size, u32 min_type)
  2778. {
  2779. struct btrfs_path *path;
  2780. struct extent_buffer *leaf;
  2781. struct btrfs_file_extent_item *fi;
  2782. struct btrfs_key key;
  2783. struct btrfs_key found_key;
  2784. u64 extent_start = 0;
  2785. u64 extent_num_bytes = 0;
  2786. u64 extent_offset = 0;
  2787. u64 item_end = 0;
  2788. u64 mask = root->sectorsize - 1;
  2789. u32 found_type = (u8)-1;
  2790. int found_extent;
  2791. int del_item;
  2792. int pending_del_nr = 0;
  2793. int pending_del_slot = 0;
  2794. int extent_type = -1;
  2795. int encoding;
  2796. int ret;
  2797. int err = 0;
  2798. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2799. if (root->ref_cows)
  2800. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2801. path = btrfs_alloc_path();
  2802. BUG_ON(!path);
  2803. path->reada = -1;
  2804. key.objectid = inode->i_ino;
  2805. key.offset = (u64)-1;
  2806. key.type = (u8)-1;
  2807. search_again:
  2808. path->leave_spinning = 1;
  2809. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2810. if (ret < 0) {
  2811. err = ret;
  2812. goto out;
  2813. }
  2814. if (ret > 0) {
  2815. /* there are no items in the tree for us to truncate, we're
  2816. * done
  2817. */
  2818. if (path->slots[0] == 0)
  2819. goto out;
  2820. path->slots[0]--;
  2821. }
  2822. while (1) {
  2823. fi = NULL;
  2824. leaf = path->nodes[0];
  2825. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2826. found_type = btrfs_key_type(&found_key);
  2827. encoding = 0;
  2828. if (found_key.objectid != inode->i_ino)
  2829. break;
  2830. if (found_type < min_type)
  2831. break;
  2832. item_end = found_key.offset;
  2833. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2834. fi = btrfs_item_ptr(leaf, path->slots[0],
  2835. struct btrfs_file_extent_item);
  2836. extent_type = btrfs_file_extent_type(leaf, fi);
  2837. encoding = btrfs_file_extent_compression(leaf, fi);
  2838. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2839. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2840. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2841. item_end +=
  2842. btrfs_file_extent_num_bytes(leaf, fi);
  2843. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2844. item_end += btrfs_file_extent_inline_len(leaf,
  2845. fi);
  2846. }
  2847. item_end--;
  2848. }
  2849. if (found_type > min_type) {
  2850. del_item = 1;
  2851. } else {
  2852. if (item_end < new_size)
  2853. break;
  2854. if (found_key.offset >= new_size)
  2855. del_item = 1;
  2856. else
  2857. del_item = 0;
  2858. }
  2859. found_extent = 0;
  2860. /* FIXME, shrink the extent if the ref count is only 1 */
  2861. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2862. goto delete;
  2863. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2864. u64 num_dec;
  2865. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2866. if (!del_item && !encoding) {
  2867. u64 orig_num_bytes =
  2868. btrfs_file_extent_num_bytes(leaf, fi);
  2869. extent_num_bytes = new_size -
  2870. found_key.offset + root->sectorsize - 1;
  2871. extent_num_bytes = extent_num_bytes &
  2872. ~((u64)root->sectorsize - 1);
  2873. btrfs_set_file_extent_num_bytes(leaf, fi,
  2874. extent_num_bytes);
  2875. num_dec = (orig_num_bytes -
  2876. extent_num_bytes);
  2877. if (root->ref_cows && extent_start != 0)
  2878. inode_sub_bytes(inode, num_dec);
  2879. btrfs_mark_buffer_dirty(leaf);
  2880. } else {
  2881. extent_num_bytes =
  2882. btrfs_file_extent_disk_num_bytes(leaf,
  2883. fi);
  2884. extent_offset = found_key.offset -
  2885. btrfs_file_extent_offset(leaf, fi);
  2886. /* FIXME blocksize != 4096 */
  2887. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2888. if (extent_start != 0) {
  2889. found_extent = 1;
  2890. if (root->ref_cows)
  2891. inode_sub_bytes(inode, num_dec);
  2892. }
  2893. }
  2894. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2895. /*
  2896. * we can't truncate inline items that have had
  2897. * special encodings
  2898. */
  2899. if (!del_item &&
  2900. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2901. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2902. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2903. u32 size = new_size - found_key.offset;
  2904. if (root->ref_cows) {
  2905. inode_sub_bytes(inode, item_end + 1 -
  2906. new_size);
  2907. }
  2908. size =
  2909. btrfs_file_extent_calc_inline_size(size);
  2910. ret = btrfs_truncate_item(trans, root, path,
  2911. size, 1);
  2912. BUG_ON(ret);
  2913. } else if (root->ref_cows) {
  2914. inode_sub_bytes(inode, item_end + 1 -
  2915. found_key.offset);
  2916. }
  2917. }
  2918. delete:
  2919. if (del_item) {
  2920. if (!pending_del_nr) {
  2921. /* no pending yet, add ourselves */
  2922. pending_del_slot = path->slots[0];
  2923. pending_del_nr = 1;
  2924. } else if (pending_del_nr &&
  2925. path->slots[0] + 1 == pending_del_slot) {
  2926. /* hop on the pending chunk */
  2927. pending_del_nr++;
  2928. pending_del_slot = path->slots[0];
  2929. } else {
  2930. BUG();
  2931. }
  2932. } else {
  2933. break;
  2934. }
  2935. if (found_extent && root->ref_cows) {
  2936. btrfs_set_path_blocking(path);
  2937. ret = btrfs_free_extent(trans, root, extent_start,
  2938. extent_num_bytes, 0,
  2939. btrfs_header_owner(leaf),
  2940. inode->i_ino, extent_offset);
  2941. BUG_ON(ret);
  2942. }
  2943. if (found_type == BTRFS_INODE_ITEM_KEY)
  2944. break;
  2945. if (path->slots[0] == 0 ||
  2946. path->slots[0] != pending_del_slot) {
  2947. if (root->ref_cows) {
  2948. err = -EAGAIN;
  2949. goto out;
  2950. }
  2951. if (pending_del_nr) {
  2952. ret = btrfs_del_items(trans, root, path,
  2953. pending_del_slot,
  2954. pending_del_nr);
  2955. BUG_ON(ret);
  2956. pending_del_nr = 0;
  2957. }
  2958. btrfs_release_path(root, path);
  2959. goto search_again;
  2960. } else {
  2961. path->slots[0]--;
  2962. }
  2963. }
  2964. out:
  2965. if (pending_del_nr) {
  2966. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2967. pending_del_nr);
  2968. BUG_ON(ret);
  2969. }
  2970. btrfs_free_path(path);
  2971. return err;
  2972. }
  2973. /*
  2974. * taken from block_truncate_page, but does cow as it zeros out
  2975. * any bytes left in the last page in the file.
  2976. */
  2977. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2978. {
  2979. struct inode *inode = mapping->host;
  2980. struct btrfs_root *root = BTRFS_I(inode)->root;
  2981. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2982. struct btrfs_ordered_extent *ordered;
  2983. struct extent_state *cached_state = NULL;
  2984. char *kaddr;
  2985. u32 blocksize = root->sectorsize;
  2986. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2987. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2988. struct page *page;
  2989. int ret = 0;
  2990. u64 page_start;
  2991. u64 page_end;
  2992. if ((offset & (blocksize - 1)) == 0)
  2993. goto out;
  2994. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  2995. if (ret)
  2996. goto out;
  2997. ret = -ENOMEM;
  2998. again:
  2999. page = grab_cache_page(mapping, index);
  3000. if (!page) {
  3001. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3002. goto out;
  3003. }
  3004. page_start = page_offset(page);
  3005. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3006. if (!PageUptodate(page)) {
  3007. ret = btrfs_readpage(NULL, page);
  3008. lock_page(page);
  3009. if (page->mapping != mapping) {
  3010. unlock_page(page);
  3011. page_cache_release(page);
  3012. goto again;
  3013. }
  3014. if (!PageUptodate(page)) {
  3015. ret = -EIO;
  3016. goto out_unlock;
  3017. }
  3018. }
  3019. wait_on_page_writeback(page);
  3020. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  3021. GFP_NOFS);
  3022. set_page_extent_mapped(page);
  3023. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3024. if (ordered) {
  3025. unlock_extent_cached(io_tree, page_start, page_end,
  3026. &cached_state, GFP_NOFS);
  3027. unlock_page(page);
  3028. page_cache_release(page);
  3029. btrfs_start_ordered_extent(inode, ordered, 1);
  3030. btrfs_put_ordered_extent(ordered);
  3031. goto again;
  3032. }
  3033. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3034. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  3035. 0, 0, &cached_state, GFP_NOFS);
  3036. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3037. &cached_state);
  3038. if (ret) {
  3039. unlock_extent_cached(io_tree, page_start, page_end,
  3040. &cached_state, GFP_NOFS);
  3041. goto out_unlock;
  3042. }
  3043. ret = 0;
  3044. if (offset != PAGE_CACHE_SIZE) {
  3045. kaddr = kmap(page);
  3046. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  3047. flush_dcache_page(page);
  3048. kunmap(page);
  3049. }
  3050. ClearPageChecked(page);
  3051. set_page_dirty(page);
  3052. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3053. GFP_NOFS);
  3054. out_unlock:
  3055. if (ret)
  3056. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3057. unlock_page(page);
  3058. page_cache_release(page);
  3059. out:
  3060. return ret;
  3061. }
  3062. int btrfs_cont_expand(struct inode *inode, loff_t size)
  3063. {
  3064. struct btrfs_trans_handle *trans;
  3065. struct btrfs_root *root = BTRFS_I(inode)->root;
  3066. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3067. struct extent_map *em = NULL;
  3068. struct extent_state *cached_state = NULL;
  3069. u64 mask = root->sectorsize - 1;
  3070. u64 hole_start = (inode->i_size + mask) & ~mask;
  3071. u64 block_end = (size + mask) & ~mask;
  3072. u64 last_byte;
  3073. u64 cur_offset;
  3074. u64 hole_size;
  3075. int err = 0;
  3076. if (size <= hole_start)
  3077. return 0;
  3078. while (1) {
  3079. struct btrfs_ordered_extent *ordered;
  3080. btrfs_wait_ordered_range(inode, hole_start,
  3081. block_end - hole_start);
  3082. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3083. &cached_state, GFP_NOFS);
  3084. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3085. if (!ordered)
  3086. break;
  3087. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3088. &cached_state, GFP_NOFS);
  3089. btrfs_put_ordered_extent(ordered);
  3090. }
  3091. cur_offset = hole_start;
  3092. while (1) {
  3093. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3094. block_end - cur_offset, 0);
  3095. BUG_ON(IS_ERR(em) || !em);
  3096. last_byte = min(extent_map_end(em), block_end);
  3097. last_byte = (last_byte + mask) & ~mask;
  3098. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3099. u64 hint_byte = 0;
  3100. hole_size = last_byte - cur_offset;
  3101. trans = btrfs_start_transaction(root, 2);
  3102. if (IS_ERR(trans)) {
  3103. err = PTR_ERR(trans);
  3104. break;
  3105. }
  3106. btrfs_set_trans_block_group(trans, inode);
  3107. err = btrfs_drop_extents(trans, inode, cur_offset,
  3108. cur_offset + hole_size,
  3109. &hint_byte, 1);
  3110. BUG_ON(err);
  3111. err = btrfs_insert_file_extent(trans, root,
  3112. inode->i_ino, cur_offset, 0,
  3113. 0, hole_size, 0, hole_size,
  3114. 0, 0, 0);
  3115. BUG_ON(err);
  3116. btrfs_drop_extent_cache(inode, hole_start,
  3117. last_byte - 1, 0);
  3118. btrfs_end_transaction(trans, root);
  3119. }
  3120. free_extent_map(em);
  3121. em = NULL;
  3122. cur_offset = last_byte;
  3123. if (cur_offset >= block_end)
  3124. break;
  3125. }
  3126. free_extent_map(em);
  3127. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3128. GFP_NOFS);
  3129. return err;
  3130. }
  3131. static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
  3132. {
  3133. struct btrfs_root *root = BTRFS_I(inode)->root;
  3134. struct btrfs_trans_handle *trans;
  3135. unsigned long nr;
  3136. int ret;
  3137. if (attr->ia_size == inode->i_size)
  3138. return 0;
  3139. if (attr->ia_size > inode->i_size) {
  3140. unsigned long limit;
  3141. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  3142. if (attr->ia_size > inode->i_sb->s_maxbytes)
  3143. return -EFBIG;
  3144. if (limit != RLIM_INFINITY && attr->ia_size > limit) {
  3145. send_sig(SIGXFSZ, current, 0);
  3146. return -EFBIG;
  3147. }
  3148. }
  3149. trans = btrfs_start_transaction(root, 5);
  3150. if (IS_ERR(trans))
  3151. return PTR_ERR(trans);
  3152. btrfs_set_trans_block_group(trans, inode);
  3153. ret = btrfs_orphan_add(trans, inode);
  3154. BUG_ON(ret);
  3155. nr = trans->blocks_used;
  3156. btrfs_end_transaction(trans, root);
  3157. btrfs_btree_balance_dirty(root, nr);
  3158. if (attr->ia_size > inode->i_size) {
  3159. ret = btrfs_cont_expand(inode, attr->ia_size);
  3160. if (ret) {
  3161. btrfs_truncate(inode);
  3162. return ret;
  3163. }
  3164. i_size_write(inode, attr->ia_size);
  3165. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  3166. trans = btrfs_start_transaction(root, 0);
  3167. BUG_ON(IS_ERR(trans));
  3168. btrfs_set_trans_block_group(trans, inode);
  3169. trans->block_rsv = root->orphan_block_rsv;
  3170. BUG_ON(!trans->block_rsv);
  3171. ret = btrfs_update_inode(trans, root, inode);
  3172. BUG_ON(ret);
  3173. if (inode->i_nlink > 0) {
  3174. ret = btrfs_orphan_del(trans, inode);
  3175. BUG_ON(ret);
  3176. }
  3177. nr = trans->blocks_used;
  3178. btrfs_end_transaction(trans, root);
  3179. btrfs_btree_balance_dirty(root, nr);
  3180. return 0;
  3181. }
  3182. /*
  3183. * We're truncating a file that used to have good data down to
  3184. * zero. Make sure it gets into the ordered flush list so that
  3185. * any new writes get down to disk quickly.
  3186. */
  3187. if (attr->ia_size == 0)
  3188. BTRFS_I(inode)->ordered_data_close = 1;
  3189. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3190. ret = vmtruncate(inode, attr->ia_size);
  3191. BUG_ON(ret);
  3192. return 0;
  3193. }
  3194. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3195. {
  3196. struct inode *inode = dentry->d_inode;
  3197. int err;
  3198. err = inode_change_ok(inode, attr);
  3199. if (err)
  3200. return err;
  3201. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3202. err = btrfs_setattr_size(inode, attr);
  3203. if (err)
  3204. return err;
  3205. }
  3206. attr->ia_valid &= ~ATTR_SIZE;
  3207. if (attr->ia_valid)
  3208. err = inode_setattr(inode, attr);
  3209. if (!err && ((attr->ia_valid & ATTR_MODE)))
  3210. err = btrfs_acl_chmod(inode);
  3211. return err;
  3212. }
  3213. void btrfs_delete_inode(struct inode *inode)
  3214. {
  3215. struct btrfs_trans_handle *trans;
  3216. struct btrfs_root *root = BTRFS_I(inode)->root;
  3217. unsigned long nr;
  3218. int ret;
  3219. truncate_inode_pages(&inode->i_data, 0);
  3220. if (is_bad_inode(inode)) {
  3221. btrfs_orphan_del(NULL, inode);
  3222. goto no_delete;
  3223. }
  3224. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3225. if (root->fs_info->log_root_recovering) {
  3226. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3227. goto no_delete;
  3228. }
  3229. if (inode->i_nlink > 0) {
  3230. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3231. goto no_delete;
  3232. }
  3233. btrfs_i_size_write(inode, 0);
  3234. while (1) {
  3235. trans = btrfs_start_transaction(root, 0);
  3236. BUG_ON(IS_ERR(trans));
  3237. btrfs_set_trans_block_group(trans, inode);
  3238. trans->block_rsv = root->orphan_block_rsv;
  3239. ret = btrfs_block_rsv_check(trans, root,
  3240. root->orphan_block_rsv, 0, 5);
  3241. if (ret) {
  3242. BUG_ON(ret != -EAGAIN);
  3243. ret = btrfs_commit_transaction(trans, root);
  3244. BUG_ON(ret);
  3245. continue;
  3246. }
  3247. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3248. if (ret != -EAGAIN)
  3249. break;
  3250. nr = trans->blocks_used;
  3251. btrfs_end_transaction(trans, root);
  3252. trans = NULL;
  3253. btrfs_btree_balance_dirty(root, nr);
  3254. }
  3255. if (ret == 0) {
  3256. ret = btrfs_orphan_del(trans, inode);
  3257. BUG_ON(ret);
  3258. }
  3259. nr = trans->blocks_used;
  3260. btrfs_end_transaction(trans, root);
  3261. btrfs_btree_balance_dirty(root, nr);
  3262. no_delete:
  3263. clear_inode(inode);
  3264. return;
  3265. }
  3266. /*
  3267. * this returns the key found in the dir entry in the location pointer.
  3268. * If no dir entries were found, location->objectid is 0.
  3269. */
  3270. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3271. struct btrfs_key *location)
  3272. {
  3273. const char *name = dentry->d_name.name;
  3274. int namelen = dentry->d_name.len;
  3275. struct btrfs_dir_item *di;
  3276. struct btrfs_path *path;
  3277. struct btrfs_root *root = BTRFS_I(dir)->root;
  3278. int ret = 0;
  3279. path = btrfs_alloc_path();
  3280. BUG_ON(!path);
  3281. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3282. namelen, 0);
  3283. if (IS_ERR(di))
  3284. ret = PTR_ERR(di);
  3285. if (!di || IS_ERR(di))
  3286. goto out_err;
  3287. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3288. out:
  3289. btrfs_free_path(path);
  3290. return ret;
  3291. out_err:
  3292. location->objectid = 0;
  3293. goto out;
  3294. }
  3295. /*
  3296. * when we hit a tree root in a directory, the btrfs part of the inode
  3297. * needs to be changed to reflect the root directory of the tree root. This
  3298. * is kind of like crossing a mount point.
  3299. */
  3300. static int fixup_tree_root_location(struct btrfs_root *root,
  3301. struct inode *dir,
  3302. struct dentry *dentry,
  3303. struct btrfs_key *location,
  3304. struct btrfs_root **sub_root)
  3305. {
  3306. struct btrfs_path *path;
  3307. struct btrfs_root *new_root;
  3308. struct btrfs_root_ref *ref;
  3309. struct extent_buffer *leaf;
  3310. int ret;
  3311. int err = 0;
  3312. path = btrfs_alloc_path();
  3313. if (!path) {
  3314. err = -ENOMEM;
  3315. goto out;
  3316. }
  3317. err = -ENOENT;
  3318. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3319. BTRFS_I(dir)->root->root_key.objectid,
  3320. location->objectid);
  3321. if (ret) {
  3322. if (ret < 0)
  3323. err = ret;
  3324. goto out;
  3325. }
  3326. leaf = path->nodes[0];
  3327. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3328. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3329. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3330. goto out;
  3331. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3332. (unsigned long)(ref + 1),
  3333. dentry->d_name.len);
  3334. if (ret)
  3335. goto out;
  3336. btrfs_release_path(root->fs_info->tree_root, path);
  3337. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3338. if (IS_ERR(new_root)) {
  3339. err = PTR_ERR(new_root);
  3340. goto out;
  3341. }
  3342. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3343. err = -ENOENT;
  3344. goto out;
  3345. }
  3346. *sub_root = new_root;
  3347. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3348. location->type = BTRFS_INODE_ITEM_KEY;
  3349. location->offset = 0;
  3350. err = 0;
  3351. out:
  3352. btrfs_free_path(path);
  3353. return err;
  3354. }
  3355. static void inode_tree_add(struct inode *inode)
  3356. {
  3357. struct btrfs_root *root = BTRFS_I(inode)->root;
  3358. struct btrfs_inode *entry;
  3359. struct rb_node **p;
  3360. struct rb_node *parent;
  3361. again:
  3362. p = &root->inode_tree.rb_node;
  3363. parent = NULL;
  3364. if (hlist_unhashed(&inode->i_hash))
  3365. return;
  3366. spin_lock(&root->inode_lock);
  3367. while (*p) {
  3368. parent = *p;
  3369. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3370. if (inode->i_ino < entry->vfs_inode.i_ino)
  3371. p = &parent->rb_left;
  3372. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3373. p = &parent->rb_right;
  3374. else {
  3375. WARN_ON(!(entry->vfs_inode.i_state &
  3376. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  3377. rb_erase(parent, &root->inode_tree);
  3378. RB_CLEAR_NODE(parent);
  3379. spin_unlock(&root->inode_lock);
  3380. goto again;
  3381. }
  3382. }
  3383. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3384. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3385. spin_unlock(&root->inode_lock);
  3386. }
  3387. static void inode_tree_del(struct inode *inode)
  3388. {
  3389. struct btrfs_root *root = BTRFS_I(inode)->root;
  3390. int empty = 0;
  3391. spin_lock(&root->inode_lock);
  3392. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3393. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3394. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3395. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3396. }
  3397. spin_unlock(&root->inode_lock);
  3398. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  3399. synchronize_srcu(&root->fs_info->subvol_srcu);
  3400. spin_lock(&root->inode_lock);
  3401. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3402. spin_unlock(&root->inode_lock);
  3403. if (empty)
  3404. btrfs_add_dead_root(root);
  3405. }
  3406. }
  3407. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3408. {
  3409. struct rb_node *node;
  3410. struct rb_node *prev;
  3411. struct btrfs_inode *entry;
  3412. struct inode *inode;
  3413. u64 objectid = 0;
  3414. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3415. spin_lock(&root->inode_lock);
  3416. again:
  3417. node = root->inode_tree.rb_node;
  3418. prev = NULL;
  3419. while (node) {
  3420. prev = node;
  3421. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3422. if (objectid < entry->vfs_inode.i_ino)
  3423. node = node->rb_left;
  3424. else if (objectid > entry->vfs_inode.i_ino)
  3425. node = node->rb_right;
  3426. else
  3427. break;
  3428. }
  3429. if (!node) {
  3430. while (prev) {
  3431. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3432. if (objectid <= entry->vfs_inode.i_ino) {
  3433. node = prev;
  3434. break;
  3435. }
  3436. prev = rb_next(prev);
  3437. }
  3438. }
  3439. while (node) {
  3440. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3441. objectid = entry->vfs_inode.i_ino + 1;
  3442. inode = igrab(&entry->vfs_inode);
  3443. if (inode) {
  3444. spin_unlock(&root->inode_lock);
  3445. if (atomic_read(&inode->i_count) > 1)
  3446. d_prune_aliases(inode);
  3447. /*
  3448. * btrfs_drop_inode will remove it from
  3449. * the inode cache when its usage count
  3450. * hits zero.
  3451. */
  3452. iput(inode);
  3453. cond_resched();
  3454. spin_lock(&root->inode_lock);
  3455. goto again;
  3456. }
  3457. if (cond_resched_lock(&root->inode_lock))
  3458. goto again;
  3459. node = rb_next(node);
  3460. }
  3461. spin_unlock(&root->inode_lock);
  3462. return 0;
  3463. }
  3464. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3465. {
  3466. struct btrfs_iget_args *args = p;
  3467. inode->i_ino = args->ino;
  3468. BTRFS_I(inode)->root = args->root;
  3469. btrfs_set_inode_space_info(args->root, inode);
  3470. return 0;
  3471. }
  3472. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3473. {
  3474. struct btrfs_iget_args *args = opaque;
  3475. return args->ino == inode->i_ino &&
  3476. args->root == BTRFS_I(inode)->root;
  3477. }
  3478. static struct inode *btrfs_iget_locked(struct super_block *s,
  3479. u64 objectid,
  3480. struct btrfs_root *root)
  3481. {
  3482. struct inode *inode;
  3483. struct btrfs_iget_args args;
  3484. args.ino = objectid;
  3485. args.root = root;
  3486. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3487. btrfs_init_locked_inode,
  3488. (void *)&args);
  3489. return inode;
  3490. }
  3491. /* Get an inode object given its location and corresponding root.
  3492. * Returns in *is_new if the inode was read from disk
  3493. */
  3494. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3495. struct btrfs_root *root, int *new)
  3496. {
  3497. struct inode *inode;
  3498. inode = btrfs_iget_locked(s, location->objectid, root);
  3499. if (!inode)
  3500. return ERR_PTR(-ENOMEM);
  3501. if (inode->i_state & I_NEW) {
  3502. BTRFS_I(inode)->root = root;
  3503. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3504. btrfs_read_locked_inode(inode);
  3505. inode_tree_add(inode);
  3506. unlock_new_inode(inode);
  3507. if (new)
  3508. *new = 1;
  3509. }
  3510. return inode;
  3511. }
  3512. static struct inode *new_simple_dir(struct super_block *s,
  3513. struct btrfs_key *key,
  3514. struct btrfs_root *root)
  3515. {
  3516. struct inode *inode = new_inode(s);
  3517. if (!inode)
  3518. return ERR_PTR(-ENOMEM);
  3519. BTRFS_I(inode)->root = root;
  3520. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3521. BTRFS_I(inode)->dummy_inode = 1;
  3522. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3523. inode->i_op = &simple_dir_inode_operations;
  3524. inode->i_fop = &simple_dir_operations;
  3525. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3526. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3527. return inode;
  3528. }
  3529. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3530. {
  3531. struct inode *inode;
  3532. struct btrfs_root *root = BTRFS_I(dir)->root;
  3533. struct btrfs_root *sub_root = root;
  3534. struct btrfs_key location;
  3535. int index;
  3536. int ret;
  3537. dentry->d_op = &btrfs_dentry_operations;
  3538. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3539. return ERR_PTR(-ENAMETOOLONG);
  3540. ret = btrfs_inode_by_name(dir, dentry, &location);
  3541. if (ret < 0)
  3542. return ERR_PTR(ret);
  3543. if (location.objectid == 0)
  3544. return NULL;
  3545. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3546. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3547. return inode;
  3548. }
  3549. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3550. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3551. ret = fixup_tree_root_location(root, dir, dentry,
  3552. &location, &sub_root);
  3553. if (ret < 0) {
  3554. if (ret != -ENOENT)
  3555. inode = ERR_PTR(ret);
  3556. else
  3557. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3558. } else {
  3559. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3560. }
  3561. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3562. if (root != sub_root) {
  3563. down_read(&root->fs_info->cleanup_work_sem);
  3564. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3565. btrfs_orphan_cleanup(sub_root);
  3566. up_read(&root->fs_info->cleanup_work_sem);
  3567. }
  3568. return inode;
  3569. }
  3570. static int btrfs_dentry_delete(struct dentry *dentry)
  3571. {
  3572. struct btrfs_root *root;
  3573. if (!dentry->d_inode && !IS_ROOT(dentry))
  3574. dentry = dentry->d_parent;
  3575. if (dentry->d_inode) {
  3576. root = BTRFS_I(dentry->d_inode)->root;
  3577. if (btrfs_root_refs(&root->root_item) == 0)
  3578. return 1;
  3579. }
  3580. return 0;
  3581. }
  3582. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3583. struct nameidata *nd)
  3584. {
  3585. struct inode *inode;
  3586. inode = btrfs_lookup_dentry(dir, dentry);
  3587. if (IS_ERR(inode))
  3588. return ERR_CAST(inode);
  3589. return d_splice_alias(inode, dentry);
  3590. }
  3591. static unsigned char btrfs_filetype_table[] = {
  3592. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3593. };
  3594. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3595. filldir_t filldir)
  3596. {
  3597. struct inode *inode = filp->f_dentry->d_inode;
  3598. struct btrfs_root *root = BTRFS_I(inode)->root;
  3599. struct btrfs_item *item;
  3600. struct btrfs_dir_item *di;
  3601. struct btrfs_key key;
  3602. struct btrfs_key found_key;
  3603. struct btrfs_path *path;
  3604. int ret;
  3605. u32 nritems;
  3606. struct extent_buffer *leaf;
  3607. int slot;
  3608. int advance;
  3609. unsigned char d_type;
  3610. int over = 0;
  3611. u32 di_cur;
  3612. u32 di_total;
  3613. u32 di_len;
  3614. int key_type = BTRFS_DIR_INDEX_KEY;
  3615. char tmp_name[32];
  3616. char *name_ptr;
  3617. int name_len;
  3618. /* FIXME, use a real flag for deciding about the key type */
  3619. if (root->fs_info->tree_root == root)
  3620. key_type = BTRFS_DIR_ITEM_KEY;
  3621. /* special case for "." */
  3622. if (filp->f_pos == 0) {
  3623. over = filldir(dirent, ".", 1,
  3624. 1, inode->i_ino,
  3625. DT_DIR);
  3626. if (over)
  3627. return 0;
  3628. filp->f_pos = 1;
  3629. }
  3630. /* special case for .., just use the back ref */
  3631. if (filp->f_pos == 1) {
  3632. u64 pino = parent_ino(filp->f_path.dentry);
  3633. over = filldir(dirent, "..", 2,
  3634. 2, pino, DT_DIR);
  3635. if (over)
  3636. return 0;
  3637. filp->f_pos = 2;
  3638. }
  3639. path = btrfs_alloc_path();
  3640. path->reada = 2;
  3641. btrfs_set_key_type(&key, key_type);
  3642. key.offset = filp->f_pos;
  3643. key.objectid = inode->i_ino;
  3644. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3645. if (ret < 0)
  3646. goto err;
  3647. advance = 0;
  3648. while (1) {
  3649. leaf = path->nodes[0];
  3650. nritems = btrfs_header_nritems(leaf);
  3651. slot = path->slots[0];
  3652. if (advance || slot >= nritems) {
  3653. if (slot >= nritems - 1) {
  3654. ret = btrfs_next_leaf(root, path);
  3655. if (ret)
  3656. break;
  3657. leaf = path->nodes[0];
  3658. nritems = btrfs_header_nritems(leaf);
  3659. slot = path->slots[0];
  3660. } else {
  3661. slot++;
  3662. path->slots[0]++;
  3663. }
  3664. }
  3665. advance = 1;
  3666. item = btrfs_item_nr(leaf, slot);
  3667. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3668. if (found_key.objectid != key.objectid)
  3669. break;
  3670. if (btrfs_key_type(&found_key) != key_type)
  3671. break;
  3672. if (found_key.offset < filp->f_pos)
  3673. continue;
  3674. filp->f_pos = found_key.offset;
  3675. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3676. di_cur = 0;
  3677. di_total = btrfs_item_size(leaf, item);
  3678. while (di_cur < di_total) {
  3679. struct btrfs_key location;
  3680. name_len = btrfs_dir_name_len(leaf, di);
  3681. if (name_len <= sizeof(tmp_name)) {
  3682. name_ptr = tmp_name;
  3683. } else {
  3684. name_ptr = kmalloc(name_len, GFP_NOFS);
  3685. if (!name_ptr) {
  3686. ret = -ENOMEM;
  3687. goto err;
  3688. }
  3689. }
  3690. read_extent_buffer(leaf, name_ptr,
  3691. (unsigned long)(di + 1), name_len);
  3692. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3693. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3694. /* is this a reference to our own snapshot? If so
  3695. * skip it
  3696. */
  3697. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3698. location.objectid == root->root_key.objectid) {
  3699. over = 0;
  3700. goto skip;
  3701. }
  3702. over = filldir(dirent, name_ptr, name_len,
  3703. found_key.offset, location.objectid,
  3704. d_type);
  3705. skip:
  3706. if (name_ptr != tmp_name)
  3707. kfree(name_ptr);
  3708. if (over)
  3709. goto nopos;
  3710. di_len = btrfs_dir_name_len(leaf, di) +
  3711. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3712. di_cur += di_len;
  3713. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3714. }
  3715. }
  3716. /* Reached end of directory/root. Bump pos past the last item. */
  3717. if (key_type == BTRFS_DIR_INDEX_KEY)
  3718. /*
  3719. * 32-bit glibc will use getdents64, but then strtol -
  3720. * so the last number we can serve is this.
  3721. */
  3722. filp->f_pos = 0x7fffffff;
  3723. else
  3724. filp->f_pos++;
  3725. nopos:
  3726. ret = 0;
  3727. err:
  3728. btrfs_free_path(path);
  3729. return ret;
  3730. }
  3731. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3732. {
  3733. struct btrfs_root *root = BTRFS_I(inode)->root;
  3734. struct btrfs_trans_handle *trans;
  3735. int ret = 0;
  3736. if (BTRFS_I(inode)->dummy_inode)
  3737. return 0;
  3738. if (wbc->sync_mode == WB_SYNC_ALL) {
  3739. trans = btrfs_join_transaction(root, 1);
  3740. btrfs_set_trans_block_group(trans, inode);
  3741. ret = btrfs_commit_transaction(trans, root);
  3742. }
  3743. return ret;
  3744. }
  3745. /*
  3746. * This is somewhat expensive, updating the tree every time the
  3747. * inode changes. But, it is most likely to find the inode in cache.
  3748. * FIXME, needs more benchmarking...there are no reasons other than performance
  3749. * to keep or drop this code.
  3750. */
  3751. void btrfs_dirty_inode(struct inode *inode)
  3752. {
  3753. struct btrfs_root *root = BTRFS_I(inode)->root;
  3754. struct btrfs_trans_handle *trans;
  3755. int ret;
  3756. if (BTRFS_I(inode)->dummy_inode)
  3757. return;
  3758. trans = btrfs_join_transaction(root, 1);
  3759. btrfs_set_trans_block_group(trans, inode);
  3760. ret = btrfs_update_inode(trans, root, inode);
  3761. if (ret)
  3762. printk(KERN_ERR"btrfs: fail to dirty inode %lu error %d\n",
  3763. inode->i_ino, ret);
  3764. btrfs_end_transaction(trans, root);
  3765. }
  3766. /*
  3767. * find the highest existing sequence number in a directory
  3768. * and then set the in-memory index_cnt variable to reflect
  3769. * free sequence numbers
  3770. */
  3771. static int btrfs_set_inode_index_count(struct inode *inode)
  3772. {
  3773. struct btrfs_root *root = BTRFS_I(inode)->root;
  3774. struct btrfs_key key, found_key;
  3775. struct btrfs_path *path;
  3776. struct extent_buffer *leaf;
  3777. int ret;
  3778. key.objectid = inode->i_ino;
  3779. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3780. key.offset = (u64)-1;
  3781. path = btrfs_alloc_path();
  3782. if (!path)
  3783. return -ENOMEM;
  3784. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3785. if (ret < 0)
  3786. goto out;
  3787. /* FIXME: we should be able to handle this */
  3788. if (ret == 0)
  3789. goto out;
  3790. ret = 0;
  3791. /*
  3792. * MAGIC NUMBER EXPLANATION:
  3793. * since we search a directory based on f_pos we have to start at 2
  3794. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3795. * else has to start at 2
  3796. */
  3797. if (path->slots[0] == 0) {
  3798. BTRFS_I(inode)->index_cnt = 2;
  3799. goto out;
  3800. }
  3801. path->slots[0]--;
  3802. leaf = path->nodes[0];
  3803. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3804. if (found_key.objectid != inode->i_ino ||
  3805. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3806. BTRFS_I(inode)->index_cnt = 2;
  3807. goto out;
  3808. }
  3809. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3810. out:
  3811. btrfs_free_path(path);
  3812. return ret;
  3813. }
  3814. /*
  3815. * helper to find a free sequence number in a given directory. This current
  3816. * code is very simple, later versions will do smarter things in the btree
  3817. */
  3818. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3819. {
  3820. int ret = 0;
  3821. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3822. ret = btrfs_set_inode_index_count(dir);
  3823. if (ret)
  3824. return ret;
  3825. }
  3826. *index = BTRFS_I(dir)->index_cnt;
  3827. BTRFS_I(dir)->index_cnt++;
  3828. return ret;
  3829. }
  3830. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3831. struct btrfs_root *root,
  3832. struct inode *dir,
  3833. const char *name, int name_len,
  3834. u64 ref_objectid, u64 objectid,
  3835. u64 alloc_hint, int mode, u64 *index)
  3836. {
  3837. struct inode *inode;
  3838. struct btrfs_inode_item *inode_item;
  3839. struct btrfs_key *location;
  3840. struct btrfs_path *path;
  3841. struct btrfs_inode_ref *ref;
  3842. struct btrfs_key key[2];
  3843. u32 sizes[2];
  3844. unsigned long ptr;
  3845. int ret;
  3846. int owner;
  3847. path = btrfs_alloc_path();
  3848. BUG_ON(!path);
  3849. inode = new_inode(root->fs_info->sb);
  3850. if (!inode)
  3851. return ERR_PTR(-ENOMEM);
  3852. if (dir) {
  3853. ret = btrfs_set_inode_index(dir, index);
  3854. if (ret) {
  3855. iput(inode);
  3856. return ERR_PTR(ret);
  3857. }
  3858. }
  3859. /*
  3860. * index_cnt is ignored for everything but a dir,
  3861. * btrfs_get_inode_index_count has an explanation for the magic
  3862. * number
  3863. */
  3864. BTRFS_I(inode)->index_cnt = 2;
  3865. BTRFS_I(inode)->root = root;
  3866. BTRFS_I(inode)->generation = trans->transid;
  3867. btrfs_set_inode_space_info(root, inode);
  3868. if (mode & S_IFDIR)
  3869. owner = 0;
  3870. else
  3871. owner = 1;
  3872. BTRFS_I(inode)->block_group =
  3873. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3874. key[0].objectid = objectid;
  3875. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3876. key[0].offset = 0;
  3877. key[1].objectid = objectid;
  3878. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3879. key[1].offset = ref_objectid;
  3880. sizes[0] = sizeof(struct btrfs_inode_item);
  3881. sizes[1] = name_len + sizeof(*ref);
  3882. path->leave_spinning = 1;
  3883. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3884. if (ret != 0)
  3885. goto fail;
  3886. inode->i_uid = current_fsuid();
  3887. if (dir && (dir->i_mode & S_ISGID)) {
  3888. inode->i_gid = dir->i_gid;
  3889. if (S_ISDIR(mode))
  3890. mode |= S_ISGID;
  3891. } else
  3892. inode->i_gid = current_fsgid();
  3893. inode->i_mode = mode;
  3894. inode->i_ino = objectid;
  3895. inode_set_bytes(inode, 0);
  3896. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3897. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3898. struct btrfs_inode_item);
  3899. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3900. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3901. struct btrfs_inode_ref);
  3902. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3903. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3904. ptr = (unsigned long)(ref + 1);
  3905. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3906. btrfs_mark_buffer_dirty(path->nodes[0]);
  3907. btrfs_free_path(path);
  3908. location = &BTRFS_I(inode)->location;
  3909. location->objectid = objectid;
  3910. location->offset = 0;
  3911. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3912. btrfs_inherit_iflags(inode, dir);
  3913. if ((mode & S_IFREG)) {
  3914. if (btrfs_test_opt(root, NODATASUM))
  3915. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3916. if (btrfs_test_opt(root, NODATACOW))
  3917. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3918. }
  3919. insert_inode_hash(inode);
  3920. inode_tree_add(inode);
  3921. return inode;
  3922. fail:
  3923. if (dir)
  3924. BTRFS_I(dir)->index_cnt--;
  3925. btrfs_free_path(path);
  3926. iput(inode);
  3927. return ERR_PTR(ret);
  3928. }
  3929. static inline u8 btrfs_inode_type(struct inode *inode)
  3930. {
  3931. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3932. }
  3933. /*
  3934. * utility function to add 'inode' into 'parent_inode' with
  3935. * a give name and a given sequence number.
  3936. * if 'add_backref' is true, also insert a backref from the
  3937. * inode to the parent directory.
  3938. */
  3939. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3940. struct inode *parent_inode, struct inode *inode,
  3941. const char *name, int name_len, int add_backref, u64 index)
  3942. {
  3943. int ret = 0;
  3944. struct btrfs_key key;
  3945. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3946. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3947. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  3948. } else {
  3949. key.objectid = inode->i_ino;
  3950. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3951. key.offset = 0;
  3952. }
  3953. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3954. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  3955. key.objectid, root->root_key.objectid,
  3956. parent_inode->i_ino,
  3957. index, name, name_len);
  3958. } else if (add_backref) {
  3959. ret = btrfs_insert_inode_ref(trans, root,
  3960. name, name_len, inode->i_ino,
  3961. parent_inode->i_ino, index);
  3962. }
  3963. if (ret == 0) {
  3964. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3965. parent_inode->i_ino, &key,
  3966. btrfs_inode_type(inode), index);
  3967. BUG_ON(ret);
  3968. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3969. name_len * 2);
  3970. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3971. ret = btrfs_update_inode(trans, root, parent_inode);
  3972. }
  3973. return ret;
  3974. }
  3975. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3976. struct dentry *dentry, struct inode *inode,
  3977. int backref, u64 index)
  3978. {
  3979. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3980. inode, dentry->d_name.name,
  3981. dentry->d_name.len, backref, index);
  3982. if (!err) {
  3983. d_instantiate(dentry, inode);
  3984. return 0;
  3985. }
  3986. if (err > 0)
  3987. err = -EEXIST;
  3988. return err;
  3989. }
  3990. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3991. int mode, dev_t rdev)
  3992. {
  3993. struct btrfs_trans_handle *trans;
  3994. struct btrfs_root *root = BTRFS_I(dir)->root;
  3995. struct inode *inode = NULL;
  3996. int err;
  3997. int drop_inode = 0;
  3998. u64 objectid;
  3999. unsigned long nr = 0;
  4000. u64 index = 0;
  4001. if (!new_valid_dev(rdev))
  4002. return -EINVAL;
  4003. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4004. if (err)
  4005. return err;
  4006. /*
  4007. * 2 for inode item and ref
  4008. * 2 for dir items
  4009. * 1 for xattr if selinux is on
  4010. */
  4011. trans = btrfs_start_transaction(root, 5);
  4012. if (IS_ERR(trans))
  4013. return PTR_ERR(trans);
  4014. btrfs_set_trans_block_group(trans, dir);
  4015. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4016. dentry->d_name.len,
  4017. dentry->d_parent->d_inode->i_ino, objectid,
  4018. BTRFS_I(dir)->block_group, mode, &index);
  4019. err = PTR_ERR(inode);
  4020. if (IS_ERR(inode))
  4021. goto out_unlock;
  4022. err = btrfs_init_inode_security(trans, inode, dir);
  4023. if (err) {
  4024. drop_inode = 1;
  4025. goto out_unlock;
  4026. }
  4027. btrfs_set_trans_block_group(trans, inode);
  4028. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4029. if (err)
  4030. drop_inode = 1;
  4031. else {
  4032. inode->i_op = &btrfs_special_inode_operations;
  4033. init_special_inode(inode, inode->i_mode, rdev);
  4034. btrfs_update_inode(trans, root, inode);
  4035. }
  4036. btrfs_update_inode_block_group(trans, inode);
  4037. btrfs_update_inode_block_group(trans, dir);
  4038. out_unlock:
  4039. nr = trans->blocks_used;
  4040. btrfs_end_transaction_throttle(trans, root);
  4041. btrfs_btree_balance_dirty(root, nr);
  4042. if (drop_inode) {
  4043. inode_dec_link_count(inode);
  4044. iput(inode);
  4045. }
  4046. return err;
  4047. }
  4048. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4049. int mode, struct nameidata *nd)
  4050. {
  4051. struct btrfs_trans_handle *trans;
  4052. struct btrfs_root *root = BTRFS_I(dir)->root;
  4053. struct inode *inode = NULL;
  4054. int drop_inode = 0;
  4055. int err;
  4056. unsigned long nr = 0;
  4057. u64 objectid;
  4058. u64 index = 0;
  4059. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4060. if (err)
  4061. return err;
  4062. /*
  4063. * 2 for inode item and ref
  4064. * 2 for dir items
  4065. * 1 for xattr if selinux is on
  4066. */
  4067. trans = btrfs_start_transaction(root, 5);
  4068. if (IS_ERR(trans))
  4069. return PTR_ERR(trans);
  4070. btrfs_set_trans_block_group(trans, dir);
  4071. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4072. dentry->d_name.len,
  4073. dentry->d_parent->d_inode->i_ino,
  4074. objectid, BTRFS_I(dir)->block_group, mode,
  4075. &index);
  4076. err = PTR_ERR(inode);
  4077. if (IS_ERR(inode))
  4078. goto out_unlock;
  4079. err = btrfs_init_inode_security(trans, inode, dir);
  4080. if (err) {
  4081. drop_inode = 1;
  4082. goto out_unlock;
  4083. }
  4084. btrfs_set_trans_block_group(trans, inode);
  4085. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4086. if (err)
  4087. drop_inode = 1;
  4088. else {
  4089. inode->i_mapping->a_ops = &btrfs_aops;
  4090. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4091. inode->i_fop = &btrfs_file_operations;
  4092. inode->i_op = &btrfs_file_inode_operations;
  4093. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4094. }
  4095. btrfs_update_inode_block_group(trans, inode);
  4096. btrfs_update_inode_block_group(trans, dir);
  4097. out_unlock:
  4098. nr = trans->blocks_used;
  4099. btrfs_end_transaction_throttle(trans, root);
  4100. if (drop_inode) {
  4101. inode_dec_link_count(inode);
  4102. iput(inode);
  4103. }
  4104. btrfs_btree_balance_dirty(root, nr);
  4105. return err;
  4106. }
  4107. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4108. struct dentry *dentry)
  4109. {
  4110. struct btrfs_trans_handle *trans;
  4111. struct btrfs_root *root = BTRFS_I(dir)->root;
  4112. struct inode *inode = old_dentry->d_inode;
  4113. u64 index;
  4114. unsigned long nr = 0;
  4115. int err;
  4116. int drop_inode = 0;
  4117. if (inode->i_nlink == 0)
  4118. return -ENOENT;
  4119. /* do not allow sys_link's with other subvols of the same device */
  4120. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4121. return -EPERM;
  4122. btrfs_inc_nlink(inode);
  4123. err = btrfs_set_inode_index(dir, &index);
  4124. if (err)
  4125. goto fail;
  4126. /*
  4127. * 1 item for inode ref
  4128. * 2 items for dir items
  4129. */
  4130. trans = btrfs_start_transaction(root, 3);
  4131. if (IS_ERR(trans)) {
  4132. err = PTR_ERR(trans);
  4133. goto fail;
  4134. }
  4135. btrfs_set_trans_block_group(trans, dir);
  4136. atomic_inc(&inode->i_count);
  4137. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  4138. if (err) {
  4139. drop_inode = 1;
  4140. } else {
  4141. btrfs_update_inode_block_group(trans, dir);
  4142. err = btrfs_update_inode(trans, root, inode);
  4143. BUG_ON(err);
  4144. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  4145. }
  4146. nr = trans->blocks_used;
  4147. btrfs_end_transaction_throttle(trans, root);
  4148. fail:
  4149. if (drop_inode) {
  4150. inode_dec_link_count(inode);
  4151. iput(inode);
  4152. }
  4153. btrfs_btree_balance_dirty(root, nr);
  4154. return err;
  4155. }
  4156. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  4157. {
  4158. struct inode *inode = NULL;
  4159. struct btrfs_trans_handle *trans;
  4160. struct btrfs_root *root = BTRFS_I(dir)->root;
  4161. int err = 0;
  4162. int drop_on_err = 0;
  4163. u64 objectid = 0;
  4164. u64 index = 0;
  4165. unsigned long nr = 1;
  4166. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4167. if (err)
  4168. return err;
  4169. /*
  4170. * 2 items for inode and ref
  4171. * 2 items for dir items
  4172. * 1 for xattr if selinux is on
  4173. */
  4174. trans = btrfs_start_transaction(root, 5);
  4175. if (IS_ERR(trans))
  4176. return PTR_ERR(trans);
  4177. btrfs_set_trans_block_group(trans, dir);
  4178. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4179. dentry->d_name.len,
  4180. dentry->d_parent->d_inode->i_ino, objectid,
  4181. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  4182. &index);
  4183. if (IS_ERR(inode)) {
  4184. err = PTR_ERR(inode);
  4185. goto out_fail;
  4186. }
  4187. drop_on_err = 1;
  4188. err = btrfs_init_inode_security(trans, inode, dir);
  4189. if (err)
  4190. goto out_fail;
  4191. inode->i_op = &btrfs_dir_inode_operations;
  4192. inode->i_fop = &btrfs_dir_file_operations;
  4193. btrfs_set_trans_block_group(trans, inode);
  4194. btrfs_i_size_write(inode, 0);
  4195. err = btrfs_update_inode(trans, root, inode);
  4196. if (err)
  4197. goto out_fail;
  4198. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  4199. inode, dentry->d_name.name,
  4200. dentry->d_name.len, 0, index);
  4201. if (err)
  4202. goto out_fail;
  4203. d_instantiate(dentry, inode);
  4204. drop_on_err = 0;
  4205. btrfs_update_inode_block_group(trans, inode);
  4206. btrfs_update_inode_block_group(trans, dir);
  4207. out_fail:
  4208. nr = trans->blocks_used;
  4209. btrfs_end_transaction_throttle(trans, root);
  4210. if (drop_on_err)
  4211. iput(inode);
  4212. btrfs_btree_balance_dirty(root, nr);
  4213. return err;
  4214. }
  4215. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4216. * and an extent that you want to insert, deal with overlap and insert
  4217. * the new extent into the tree.
  4218. */
  4219. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4220. struct extent_map *existing,
  4221. struct extent_map *em,
  4222. u64 map_start, u64 map_len)
  4223. {
  4224. u64 start_diff;
  4225. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4226. start_diff = map_start - em->start;
  4227. em->start = map_start;
  4228. em->len = map_len;
  4229. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4230. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4231. em->block_start += start_diff;
  4232. em->block_len -= start_diff;
  4233. }
  4234. return add_extent_mapping(em_tree, em);
  4235. }
  4236. static noinline int uncompress_inline(struct btrfs_path *path,
  4237. struct inode *inode, struct page *page,
  4238. size_t pg_offset, u64 extent_offset,
  4239. struct btrfs_file_extent_item *item)
  4240. {
  4241. int ret;
  4242. struct extent_buffer *leaf = path->nodes[0];
  4243. char *tmp;
  4244. size_t max_size;
  4245. unsigned long inline_size;
  4246. unsigned long ptr;
  4247. WARN_ON(pg_offset != 0);
  4248. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4249. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4250. btrfs_item_nr(leaf, path->slots[0]));
  4251. tmp = kmalloc(inline_size, GFP_NOFS);
  4252. ptr = btrfs_file_extent_inline_start(item);
  4253. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4254. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4255. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  4256. inline_size, max_size);
  4257. if (ret) {
  4258. char *kaddr = kmap_atomic(page, KM_USER0);
  4259. unsigned long copy_size = min_t(u64,
  4260. PAGE_CACHE_SIZE - pg_offset,
  4261. max_size - extent_offset);
  4262. memset(kaddr + pg_offset, 0, copy_size);
  4263. kunmap_atomic(kaddr, KM_USER0);
  4264. }
  4265. kfree(tmp);
  4266. return 0;
  4267. }
  4268. /*
  4269. * a bit scary, this does extent mapping from logical file offset to the disk.
  4270. * the ugly parts come from merging extents from the disk with the in-ram
  4271. * representation. This gets more complex because of the data=ordered code,
  4272. * where the in-ram extents might be locked pending data=ordered completion.
  4273. *
  4274. * This also copies inline extents directly into the page.
  4275. */
  4276. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4277. size_t pg_offset, u64 start, u64 len,
  4278. int create)
  4279. {
  4280. int ret;
  4281. int err = 0;
  4282. u64 bytenr;
  4283. u64 extent_start = 0;
  4284. u64 extent_end = 0;
  4285. u64 objectid = inode->i_ino;
  4286. u32 found_type;
  4287. struct btrfs_path *path = NULL;
  4288. struct btrfs_root *root = BTRFS_I(inode)->root;
  4289. struct btrfs_file_extent_item *item;
  4290. struct extent_buffer *leaf;
  4291. struct btrfs_key found_key;
  4292. struct extent_map *em = NULL;
  4293. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4294. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4295. struct btrfs_trans_handle *trans = NULL;
  4296. int compressed;
  4297. again:
  4298. read_lock(&em_tree->lock);
  4299. em = lookup_extent_mapping(em_tree, start, len);
  4300. if (em)
  4301. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4302. read_unlock(&em_tree->lock);
  4303. if (em) {
  4304. if (em->start > start || em->start + em->len <= start)
  4305. free_extent_map(em);
  4306. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4307. free_extent_map(em);
  4308. else
  4309. goto out;
  4310. }
  4311. em = alloc_extent_map(GFP_NOFS);
  4312. if (!em) {
  4313. err = -ENOMEM;
  4314. goto out;
  4315. }
  4316. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4317. em->start = EXTENT_MAP_HOLE;
  4318. em->orig_start = EXTENT_MAP_HOLE;
  4319. em->len = (u64)-1;
  4320. em->block_len = (u64)-1;
  4321. if (!path) {
  4322. path = btrfs_alloc_path();
  4323. BUG_ON(!path);
  4324. }
  4325. ret = btrfs_lookup_file_extent(trans, root, path,
  4326. objectid, start, trans != NULL);
  4327. if (ret < 0) {
  4328. err = ret;
  4329. goto out;
  4330. }
  4331. if (ret != 0) {
  4332. if (path->slots[0] == 0)
  4333. goto not_found;
  4334. path->slots[0]--;
  4335. }
  4336. leaf = path->nodes[0];
  4337. item = btrfs_item_ptr(leaf, path->slots[0],
  4338. struct btrfs_file_extent_item);
  4339. /* are we inside the extent that was found? */
  4340. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4341. found_type = btrfs_key_type(&found_key);
  4342. if (found_key.objectid != objectid ||
  4343. found_type != BTRFS_EXTENT_DATA_KEY) {
  4344. goto not_found;
  4345. }
  4346. found_type = btrfs_file_extent_type(leaf, item);
  4347. extent_start = found_key.offset;
  4348. compressed = btrfs_file_extent_compression(leaf, item);
  4349. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4350. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4351. extent_end = extent_start +
  4352. btrfs_file_extent_num_bytes(leaf, item);
  4353. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4354. size_t size;
  4355. size = btrfs_file_extent_inline_len(leaf, item);
  4356. extent_end = (extent_start + size + root->sectorsize - 1) &
  4357. ~((u64)root->sectorsize - 1);
  4358. }
  4359. if (start >= extent_end) {
  4360. path->slots[0]++;
  4361. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4362. ret = btrfs_next_leaf(root, path);
  4363. if (ret < 0) {
  4364. err = ret;
  4365. goto out;
  4366. }
  4367. if (ret > 0)
  4368. goto not_found;
  4369. leaf = path->nodes[0];
  4370. }
  4371. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4372. if (found_key.objectid != objectid ||
  4373. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4374. goto not_found;
  4375. if (start + len <= found_key.offset)
  4376. goto not_found;
  4377. em->start = start;
  4378. em->len = found_key.offset - start;
  4379. goto not_found_em;
  4380. }
  4381. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4382. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4383. em->start = extent_start;
  4384. em->len = extent_end - extent_start;
  4385. em->orig_start = extent_start -
  4386. btrfs_file_extent_offset(leaf, item);
  4387. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4388. if (bytenr == 0) {
  4389. em->block_start = EXTENT_MAP_HOLE;
  4390. goto insert;
  4391. }
  4392. if (compressed) {
  4393. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4394. em->block_start = bytenr;
  4395. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4396. item);
  4397. } else {
  4398. bytenr += btrfs_file_extent_offset(leaf, item);
  4399. em->block_start = bytenr;
  4400. em->block_len = em->len;
  4401. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4402. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4403. }
  4404. goto insert;
  4405. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4406. unsigned long ptr;
  4407. char *map;
  4408. size_t size;
  4409. size_t extent_offset;
  4410. size_t copy_size;
  4411. em->block_start = EXTENT_MAP_INLINE;
  4412. if (!page || create) {
  4413. em->start = extent_start;
  4414. em->len = extent_end - extent_start;
  4415. goto out;
  4416. }
  4417. size = btrfs_file_extent_inline_len(leaf, item);
  4418. extent_offset = page_offset(page) + pg_offset - extent_start;
  4419. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4420. size - extent_offset);
  4421. em->start = extent_start + extent_offset;
  4422. em->len = (copy_size + root->sectorsize - 1) &
  4423. ~((u64)root->sectorsize - 1);
  4424. em->orig_start = EXTENT_MAP_INLINE;
  4425. if (compressed)
  4426. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4427. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4428. if (create == 0 && !PageUptodate(page)) {
  4429. if (btrfs_file_extent_compression(leaf, item) ==
  4430. BTRFS_COMPRESS_ZLIB) {
  4431. ret = uncompress_inline(path, inode, page,
  4432. pg_offset,
  4433. extent_offset, item);
  4434. BUG_ON(ret);
  4435. } else {
  4436. map = kmap(page);
  4437. read_extent_buffer(leaf, map + pg_offset, ptr,
  4438. copy_size);
  4439. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4440. memset(map + pg_offset + copy_size, 0,
  4441. PAGE_CACHE_SIZE - pg_offset -
  4442. copy_size);
  4443. }
  4444. kunmap(page);
  4445. }
  4446. flush_dcache_page(page);
  4447. } else if (create && PageUptodate(page)) {
  4448. WARN_ON(1);
  4449. if (!trans) {
  4450. kunmap(page);
  4451. free_extent_map(em);
  4452. em = NULL;
  4453. btrfs_release_path(root, path);
  4454. trans = btrfs_join_transaction(root, 1);
  4455. goto again;
  4456. }
  4457. map = kmap(page);
  4458. write_extent_buffer(leaf, map + pg_offset, ptr,
  4459. copy_size);
  4460. kunmap(page);
  4461. btrfs_mark_buffer_dirty(leaf);
  4462. }
  4463. set_extent_uptodate(io_tree, em->start,
  4464. extent_map_end(em) - 1, GFP_NOFS);
  4465. goto insert;
  4466. } else {
  4467. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4468. WARN_ON(1);
  4469. }
  4470. not_found:
  4471. em->start = start;
  4472. em->len = len;
  4473. not_found_em:
  4474. em->block_start = EXTENT_MAP_HOLE;
  4475. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4476. insert:
  4477. btrfs_release_path(root, path);
  4478. if (em->start > start || extent_map_end(em) <= start) {
  4479. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4480. "[%llu %llu]\n", (unsigned long long)em->start,
  4481. (unsigned long long)em->len,
  4482. (unsigned long long)start,
  4483. (unsigned long long)len);
  4484. err = -EIO;
  4485. goto out;
  4486. }
  4487. err = 0;
  4488. write_lock(&em_tree->lock);
  4489. ret = add_extent_mapping(em_tree, em);
  4490. /* it is possible that someone inserted the extent into the tree
  4491. * while we had the lock dropped. It is also possible that
  4492. * an overlapping map exists in the tree
  4493. */
  4494. if (ret == -EEXIST) {
  4495. struct extent_map *existing;
  4496. ret = 0;
  4497. existing = lookup_extent_mapping(em_tree, start, len);
  4498. if (existing && (existing->start > start ||
  4499. existing->start + existing->len <= start)) {
  4500. free_extent_map(existing);
  4501. existing = NULL;
  4502. }
  4503. if (!existing) {
  4504. existing = lookup_extent_mapping(em_tree, em->start,
  4505. em->len);
  4506. if (existing) {
  4507. err = merge_extent_mapping(em_tree, existing,
  4508. em, start,
  4509. root->sectorsize);
  4510. free_extent_map(existing);
  4511. if (err) {
  4512. free_extent_map(em);
  4513. em = NULL;
  4514. }
  4515. } else {
  4516. err = -EIO;
  4517. free_extent_map(em);
  4518. em = NULL;
  4519. }
  4520. } else {
  4521. free_extent_map(em);
  4522. em = existing;
  4523. err = 0;
  4524. }
  4525. }
  4526. write_unlock(&em_tree->lock);
  4527. out:
  4528. if (path)
  4529. btrfs_free_path(path);
  4530. if (trans) {
  4531. ret = btrfs_end_transaction(trans, root);
  4532. if (!err)
  4533. err = ret;
  4534. }
  4535. if (err) {
  4536. free_extent_map(em);
  4537. return ERR_PTR(err);
  4538. }
  4539. return em;
  4540. }
  4541. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  4542. const struct iovec *iov, loff_t offset,
  4543. unsigned long nr_segs)
  4544. {
  4545. return -EINVAL;
  4546. }
  4547. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4548. __u64 start, __u64 len)
  4549. {
  4550. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  4551. }
  4552. int btrfs_readpage(struct file *file, struct page *page)
  4553. {
  4554. struct extent_io_tree *tree;
  4555. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4556. return extent_read_full_page(tree, page, btrfs_get_extent);
  4557. }
  4558. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  4559. {
  4560. struct extent_io_tree *tree;
  4561. if (current->flags & PF_MEMALLOC) {
  4562. redirty_page_for_writepage(wbc, page);
  4563. unlock_page(page);
  4564. return 0;
  4565. }
  4566. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4567. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  4568. }
  4569. int btrfs_writepages(struct address_space *mapping,
  4570. struct writeback_control *wbc)
  4571. {
  4572. struct extent_io_tree *tree;
  4573. tree = &BTRFS_I(mapping->host)->io_tree;
  4574. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  4575. }
  4576. static int
  4577. btrfs_readpages(struct file *file, struct address_space *mapping,
  4578. struct list_head *pages, unsigned nr_pages)
  4579. {
  4580. struct extent_io_tree *tree;
  4581. tree = &BTRFS_I(mapping->host)->io_tree;
  4582. return extent_readpages(tree, mapping, pages, nr_pages,
  4583. btrfs_get_extent);
  4584. }
  4585. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4586. {
  4587. struct extent_io_tree *tree;
  4588. struct extent_map_tree *map;
  4589. int ret;
  4590. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4591. map = &BTRFS_I(page->mapping->host)->extent_tree;
  4592. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  4593. if (ret == 1) {
  4594. ClearPagePrivate(page);
  4595. set_page_private(page, 0);
  4596. page_cache_release(page);
  4597. }
  4598. return ret;
  4599. }
  4600. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4601. {
  4602. if (PageWriteback(page) || PageDirty(page))
  4603. return 0;
  4604. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  4605. }
  4606. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  4607. {
  4608. struct extent_io_tree *tree;
  4609. struct btrfs_ordered_extent *ordered;
  4610. struct extent_state *cached_state = NULL;
  4611. u64 page_start = page_offset(page);
  4612. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  4613. /*
  4614. * we have the page locked, so new writeback can't start,
  4615. * and the dirty bit won't be cleared while we are here.
  4616. *
  4617. * Wait for IO on this page so that we can safely clear
  4618. * the PagePrivate2 bit and do ordered accounting
  4619. */
  4620. wait_on_page_writeback(page);
  4621. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4622. if (offset) {
  4623. btrfs_releasepage(page, GFP_NOFS);
  4624. return;
  4625. }
  4626. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  4627. GFP_NOFS);
  4628. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  4629. page_offset(page));
  4630. if (ordered) {
  4631. /*
  4632. * IO on this page will never be started, so we need
  4633. * to account for any ordered extents now
  4634. */
  4635. clear_extent_bit(tree, page_start, page_end,
  4636. EXTENT_DIRTY | EXTENT_DELALLOC |
  4637. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  4638. &cached_state, GFP_NOFS);
  4639. /*
  4640. * whoever cleared the private bit is responsible
  4641. * for the finish_ordered_io
  4642. */
  4643. if (TestClearPagePrivate2(page)) {
  4644. btrfs_finish_ordered_io(page->mapping->host,
  4645. page_start, page_end);
  4646. }
  4647. btrfs_put_ordered_extent(ordered);
  4648. cached_state = NULL;
  4649. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  4650. GFP_NOFS);
  4651. }
  4652. clear_extent_bit(tree, page_start, page_end,
  4653. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  4654. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  4655. __btrfs_releasepage(page, GFP_NOFS);
  4656. ClearPageChecked(page);
  4657. if (PagePrivate(page)) {
  4658. ClearPagePrivate(page);
  4659. set_page_private(page, 0);
  4660. page_cache_release(page);
  4661. }
  4662. }
  4663. /*
  4664. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  4665. * called from a page fault handler when a page is first dirtied. Hence we must
  4666. * be careful to check for EOF conditions here. We set the page up correctly
  4667. * for a written page which means we get ENOSPC checking when writing into
  4668. * holes and correct delalloc and unwritten extent mapping on filesystems that
  4669. * support these features.
  4670. *
  4671. * We are not allowed to take the i_mutex here so we have to play games to
  4672. * protect against truncate races as the page could now be beyond EOF. Because
  4673. * vmtruncate() writes the inode size before removing pages, once we have the
  4674. * page lock we can determine safely if the page is beyond EOF. If it is not
  4675. * beyond EOF, then the page is guaranteed safe against truncation until we
  4676. * unlock the page.
  4677. */
  4678. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4679. {
  4680. struct page *page = vmf->page;
  4681. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  4682. struct btrfs_root *root = BTRFS_I(inode)->root;
  4683. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4684. struct btrfs_ordered_extent *ordered;
  4685. struct extent_state *cached_state = NULL;
  4686. char *kaddr;
  4687. unsigned long zero_start;
  4688. loff_t size;
  4689. int ret;
  4690. u64 page_start;
  4691. u64 page_end;
  4692. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  4693. if (ret) {
  4694. if (ret == -ENOMEM)
  4695. ret = VM_FAULT_OOM;
  4696. else /* -ENOSPC, -EIO, etc */
  4697. ret = VM_FAULT_SIGBUS;
  4698. goto out;
  4699. }
  4700. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  4701. again:
  4702. lock_page(page);
  4703. size = i_size_read(inode);
  4704. page_start = page_offset(page);
  4705. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4706. if ((page->mapping != inode->i_mapping) ||
  4707. (page_start >= size)) {
  4708. /* page got truncated out from underneath us */
  4709. goto out_unlock;
  4710. }
  4711. wait_on_page_writeback(page);
  4712. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  4713. GFP_NOFS);
  4714. set_page_extent_mapped(page);
  4715. /*
  4716. * we can't set the delalloc bits if there are pending ordered
  4717. * extents. Drop our locks and wait for them to finish
  4718. */
  4719. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4720. if (ordered) {
  4721. unlock_extent_cached(io_tree, page_start, page_end,
  4722. &cached_state, GFP_NOFS);
  4723. unlock_page(page);
  4724. btrfs_start_ordered_extent(inode, ordered, 1);
  4725. btrfs_put_ordered_extent(ordered);
  4726. goto again;
  4727. }
  4728. /*
  4729. * XXX - page_mkwrite gets called every time the page is dirtied, even
  4730. * if it was already dirty, so for space accounting reasons we need to
  4731. * clear any delalloc bits for the range we are fixing to save. There
  4732. * is probably a better way to do this, but for now keep consistent with
  4733. * prepare_pages in the normal write path.
  4734. */
  4735. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  4736. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  4737. 0, 0, &cached_state, GFP_NOFS);
  4738. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  4739. &cached_state);
  4740. if (ret) {
  4741. unlock_extent_cached(io_tree, page_start, page_end,
  4742. &cached_state, GFP_NOFS);
  4743. ret = VM_FAULT_SIGBUS;
  4744. goto out_unlock;
  4745. }
  4746. ret = 0;
  4747. /* page is wholly or partially inside EOF */
  4748. if (page_start + PAGE_CACHE_SIZE > size)
  4749. zero_start = size & ~PAGE_CACHE_MASK;
  4750. else
  4751. zero_start = PAGE_CACHE_SIZE;
  4752. if (zero_start != PAGE_CACHE_SIZE) {
  4753. kaddr = kmap(page);
  4754. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  4755. flush_dcache_page(page);
  4756. kunmap(page);
  4757. }
  4758. ClearPageChecked(page);
  4759. set_page_dirty(page);
  4760. SetPageUptodate(page);
  4761. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  4762. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  4763. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  4764. out_unlock:
  4765. if (!ret)
  4766. return VM_FAULT_LOCKED;
  4767. unlock_page(page);
  4768. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  4769. out:
  4770. return ret;
  4771. }
  4772. static void btrfs_truncate(struct inode *inode)
  4773. {
  4774. struct btrfs_root *root = BTRFS_I(inode)->root;
  4775. int ret;
  4776. struct btrfs_trans_handle *trans;
  4777. unsigned long nr;
  4778. u64 mask = root->sectorsize - 1;
  4779. if (!S_ISREG(inode->i_mode)) {
  4780. WARN_ON(1);
  4781. return;
  4782. }
  4783. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4784. if (ret)
  4785. return;
  4786. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4787. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  4788. trans = btrfs_start_transaction(root, 0);
  4789. BUG_ON(IS_ERR(trans));
  4790. btrfs_set_trans_block_group(trans, inode);
  4791. trans->block_rsv = root->orphan_block_rsv;
  4792. /*
  4793. * setattr is responsible for setting the ordered_data_close flag,
  4794. * but that is only tested during the last file release. That
  4795. * could happen well after the next commit, leaving a great big
  4796. * window where new writes may get lost if someone chooses to write
  4797. * to this file after truncating to zero
  4798. *
  4799. * The inode doesn't have any dirty data here, and so if we commit
  4800. * this is a noop. If someone immediately starts writing to the inode
  4801. * it is very likely we'll catch some of their writes in this
  4802. * transaction, and the commit will find this file on the ordered
  4803. * data list with good things to send down.
  4804. *
  4805. * This is a best effort solution, there is still a window where
  4806. * using truncate to replace the contents of the file will
  4807. * end up with a zero length file after a crash.
  4808. */
  4809. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4810. btrfs_add_ordered_operation(trans, root, inode);
  4811. while (1) {
  4812. if (!trans) {
  4813. trans = btrfs_start_transaction(root, 0);
  4814. BUG_ON(IS_ERR(trans));
  4815. btrfs_set_trans_block_group(trans, inode);
  4816. trans->block_rsv = root->orphan_block_rsv;
  4817. }
  4818. ret = btrfs_block_rsv_check(trans, root,
  4819. root->orphan_block_rsv, 0, 5);
  4820. if (ret) {
  4821. BUG_ON(ret != -EAGAIN);
  4822. ret = btrfs_commit_transaction(trans, root);
  4823. BUG_ON(ret);
  4824. trans = NULL;
  4825. continue;
  4826. }
  4827. ret = btrfs_truncate_inode_items(trans, root, inode,
  4828. inode->i_size,
  4829. BTRFS_EXTENT_DATA_KEY);
  4830. if (ret != -EAGAIN)
  4831. break;
  4832. ret = btrfs_update_inode(trans, root, inode);
  4833. BUG_ON(ret);
  4834. nr = trans->blocks_used;
  4835. btrfs_end_transaction(trans, root);
  4836. trans = NULL;
  4837. btrfs_btree_balance_dirty(root, nr);
  4838. }
  4839. if (ret == 0 && inode->i_nlink > 0) {
  4840. ret = btrfs_orphan_del(trans, inode);
  4841. BUG_ON(ret);
  4842. }
  4843. ret = btrfs_update_inode(trans, root, inode);
  4844. BUG_ON(ret);
  4845. nr = trans->blocks_used;
  4846. ret = btrfs_end_transaction_throttle(trans, root);
  4847. BUG_ON(ret);
  4848. btrfs_btree_balance_dirty(root, nr);
  4849. }
  4850. /*
  4851. * create a new subvolume directory/inode (helper for the ioctl).
  4852. */
  4853. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4854. struct btrfs_root *new_root,
  4855. u64 new_dirid, u64 alloc_hint)
  4856. {
  4857. struct inode *inode;
  4858. int err;
  4859. u64 index = 0;
  4860. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4861. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4862. if (IS_ERR(inode))
  4863. return PTR_ERR(inode);
  4864. inode->i_op = &btrfs_dir_inode_operations;
  4865. inode->i_fop = &btrfs_dir_file_operations;
  4866. inode->i_nlink = 1;
  4867. btrfs_i_size_write(inode, 0);
  4868. err = btrfs_update_inode(trans, new_root, inode);
  4869. BUG_ON(err);
  4870. iput(inode);
  4871. return 0;
  4872. }
  4873. /* helper function for file defrag and space balancing. This
  4874. * forces readahead on a given range of bytes in an inode
  4875. */
  4876. unsigned long btrfs_force_ra(struct address_space *mapping,
  4877. struct file_ra_state *ra, struct file *file,
  4878. pgoff_t offset, pgoff_t last_index)
  4879. {
  4880. pgoff_t req_size = last_index - offset + 1;
  4881. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4882. return offset + req_size;
  4883. }
  4884. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4885. {
  4886. struct btrfs_inode *ei;
  4887. struct inode *inode;
  4888. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4889. if (!ei)
  4890. return NULL;
  4891. ei->root = NULL;
  4892. ei->space_info = NULL;
  4893. ei->generation = 0;
  4894. ei->sequence = 0;
  4895. ei->last_trans = 0;
  4896. ei->last_sub_trans = 0;
  4897. ei->logged_trans = 0;
  4898. ei->delalloc_bytes = 0;
  4899. ei->reserved_bytes = 0;
  4900. ei->disk_i_size = 0;
  4901. ei->flags = 0;
  4902. ei->index_cnt = (u64)-1;
  4903. ei->last_unlink_trans = 0;
  4904. spin_lock_init(&ei->accounting_lock);
  4905. atomic_set(&ei->outstanding_extents, 0);
  4906. ei->reserved_extents = 0;
  4907. ei->ordered_data_close = 0;
  4908. ei->orphan_meta_reserved = 0;
  4909. ei->dummy_inode = 0;
  4910. ei->force_compress = 0;
  4911. inode = &ei->vfs_inode;
  4912. extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
  4913. extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
  4914. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
  4915. mutex_init(&ei->log_mutex);
  4916. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4917. INIT_LIST_HEAD(&ei->i_orphan);
  4918. INIT_LIST_HEAD(&ei->delalloc_inodes);
  4919. INIT_LIST_HEAD(&ei->ordered_operations);
  4920. RB_CLEAR_NODE(&ei->rb_node);
  4921. return inode;
  4922. }
  4923. void btrfs_destroy_inode(struct inode *inode)
  4924. {
  4925. struct btrfs_ordered_extent *ordered;
  4926. struct btrfs_root *root = BTRFS_I(inode)->root;
  4927. WARN_ON(!list_empty(&inode->i_dentry));
  4928. WARN_ON(inode->i_data.nrpages);
  4929. WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
  4930. WARN_ON(BTRFS_I(inode)->reserved_extents);
  4931. /*
  4932. * This can happen where we create an inode, but somebody else also
  4933. * created the same inode and we need to destroy the one we already
  4934. * created.
  4935. */
  4936. if (!root)
  4937. goto free;
  4938. /*
  4939. * Make sure we're properly removed from the ordered operation
  4940. * lists.
  4941. */
  4942. smp_mb();
  4943. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4944. spin_lock(&root->fs_info->ordered_extent_lock);
  4945. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4946. spin_unlock(&root->fs_info->ordered_extent_lock);
  4947. }
  4948. spin_lock(&root->orphan_lock);
  4949. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4950. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  4951. inode->i_ino);
  4952. list_del_init(&BTRFS_I(inode)->i_orphan);
  4953. }
  4954. spin_unlock(&root->orphan_lock);
  4955. while (1) {
  4956. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4957. if (!ordered)
  4958. break;
  4959. else {
  4960. printk(KERN_ERR "btrfs found ordered "
  4961. "extent %llu %llu on inode cleanup\n",
  4962. (unsigned long long)ordered->file_offset,
  4963. (unsigned long long)ordered->len);
  4964. btrfs_remove_ordered_extent(inode, ordered);
  4965. btrfs_put_ordered_extent(ordered);
  4966. btrfs_put_ordered_extent(ordered);
  4967. }
  4968. }
  4969. inode_tree_del(inode);
  4970. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4971. free:
  4972. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4973. }
  4974. void btrfs_drop_inode(struct inode *inode)
  4975. {
  4976. struct btrfs_root *root = BTRFS_I(inode)->root;
  4977. if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
  4978. generic_delete_inode(inode);
  4979. else
  4980. generic_drop_inode(inode);
  4981. }
  4982. static void init_once(void *foo)
  4983. {
  4984. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4985. inode_init_once(&ei->vfs_inode);
  4986. }
  4987. void btrfs_destroy_cachep(void)
  4988. {
  4989. if (btrfs_inode_cachep)
  4990. kmem_cache_destroy(btrfs_inode_cachep);
  4991. if (btrfs_trans_handle_cachep)
  4992. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4993. if (btrfs_transaction_cachep)
  4994. kmem_cache_destroy(btrfs_transaction_cachep);
  4995. if (btrfs_path_cachep)
  4996. kmem_cache_destroy(btrfs_path_cachep);
  4997. }
  4998. int btrfs_init_cachep(void)
  4999. {
  5000. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  5001. sizeof(struct btrfs_inode), 0,
  5002. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  5003. if (!btrfs_inode_cachep)
  5004. goto fail;
  5005. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  5006. sizeof(struct btrfs_trans_handle), 0,
  5007. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5008. if (!btrfs_trans_handle_cachep)
  5009. goto fail;
  5010. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  5011. sizeof(struct btrfs_transaction), 0,
  5012. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5013. if (!btrfs_transaction_cachep)
  5014. goto fail;
  5015. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  5016. sizeof(struct btrfs_path), 0,
  5017. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5018. if (!btrfs_path_cachep)
  5019. goto fail;
  5020. return 0;
  5021. fail:
  5022. btrfs_destroy_cachep();
  5023. return -ENOMEM;
  5024. }
  5025. static int btrfs_getattr(struct vfsmount *mnt,
  5026. struct dentry *dentry, struct kstat *stat)
  5027. {
  5028. struct inode *inode = dentry->d_inode;
  5029. generic_fillattr(inode, stat);
  5030. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  5031. stat->blksize = PAGE_CACHE_SIZE;
  5032. stat->blocks = (inode_get_bytes(inode) +
  5033. BTRFS_I(inode)->delalloc_bytes) >> 9;
  5034. return 0;
  5035. }
  5036. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  5037. struct inode *new_dir, struct dentry *new_dentry)
  5038. {
  5039. struct btrfs_trans_handle *trans;
  5040. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  5041. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  5042. struct inode *new_inode = new_dentry->d_inode;
  5043. struct inode *old_inode = old_dentry->d_inode;
  5044. struct timespec ctime = CURRENT_TIME;
  5045. u64 index = 0;
  5046. u64 root_objectid;
  5047. int ret;
  5048. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  5049. return -EPERM;
  5050. /* we only allow rename subvolume link between subvolumes */
  5051. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  5052. return -EXDEV;
  5053. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  5054. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  5055. return -ENOTEMPTY;
  5056. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  5057. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  5058. return -ENOTEMPTY;
  5059. /*
  5060. * we're using rename to replace one file with another.
  5061. * and the replacement file is large. Start IO on it now so
  5062. * we don't add too much work to the end of the transaction
  5063. */
  5064. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  5065. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  5066. filemap_flush(old_inode->i_mapping);
  5067. /* close the racy window with snapshot create/destroy ioctl */
  5068. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  5069. down_read(&root->fs_info->subvol_sem);
  5070. /*
  5071. * We want to reserve the absolute worst case amount of items. So if
  5072. * both inodes are subvols and we need to unlink them then that would
  5073. * require 4 item modifications, but if they are both normal inodes it
  5074. * would require 5 item modifications, so we'll assume their normal
  5075. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  5076. * should cover the worst case number of items we'll modify.
  5077. */
  5078. trans = btrfs_start_transaction(root, 20);
  5079. if (IS_ERR(trans))
  5080. return PTR_ERR(trans);
  5081. btrfs_set_trans_block_group(trans, new_dir);
  5082. if (dest != root)
  5083. btrfs_record_root_in_trans(trans, dest);
  5084. ret = btrfs_set_inode_index(new_dir, &index);
  5085. if (ret)
  5086. goto out_fail;
  5087. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5088. /* force full log commit if subvolume involved. */
  5089. root->fs_info->last_trans_log_full_commit = trans->transid;
  5090. } else {
  5091. ret = btrfs_insert_inode_ref(trans, dest,
  5092. new_dentry->d_name.name,
  5093. new_dentry->d_name.len,
  5094. old_inode->i_ino,
  5095. new_dir->i_ino, index);
  5096. if (ret)
  5097. goto out_fail;
  5098. /*
  5099. * this is an ugly little race, but the rename is required
  5100. * to make sure that if we crash, the inode is either at the
  5101. * old name or the new one. pinning the log transaction lets
  5102. * us make sure we don't allow a log commit to come in after
  5103. * we unlink the name but before we add the new name back in.
  5104. */
  5105. btrfs_pin_log_trans(root);
  5106. }
  5107. /*
  5108. * make sure the inode gets flushed if it is replacing
  5109. * something.
  5110. */
  5111. if (new_inode && new_inode->i_size &&
  5112. old_inode && S_ISREG(old_inode->i_mode)) {
  5113. btrfs_add_ordered_operation(trans, root, old_inode);
  5114. }
  5115. old_dir->i_ctime = old_dir->i_mtime = ctime;
  5116. new_dir->i_ctime = new_dir->i_mtime = ctime;
  5117. old_inode->i_ctime = ctime;
  5118. if (old_dentry->d_parent != new_dentry->d_parent)
  5119. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  5120. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5121. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  5122. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  5123. old_dentry->d_name.name,
  5124. old_dentry->d_name.len);
  5125. } else {
  5126. btrfs_inc_nlink(old_dentry->d_inode);
  5127. ret = btrfs_unlink_inode(trans, root, old_dir,
  5128. old_dentry->d_inode,
  5129. old_dentry->d_name.name,
  5130. old_dentry->d_name.len);
  5131. }
  5132. BUG_ON(ret);
  5133. if (new_inode) {
  5134. new_inode->i_ctime = CURRENT_TIME;
  5135. if (unlikely(new_inode->i_ino ==
  5136. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  5137. root_objectid = BTRFS_I(new_inode)->location.objectid;
  5138. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  5139. root_objectid,
  5140. new_dentry->d_name.name,
  5141. new_dentry->d_name.len);
  5142. BUG_ON(new_inode->i_nlink == 0);
  5143. } else {
  5144. ret = btrfs_unlink_inode(trans, dest, new_dir,
  5145. new_dentry->d_inode,
  5146. new_dentry->d_name.name,
  5147. new_dentry->d_name.len);
  5148. }
  5149. BUG_ON(ret);
  5150. if (new_inode->i_nlink == 0) {
  5151. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  5152. BUG_ON(ret);
  5153. }
  5154. }
  5155. ret = btrfs_add_link(trans, new_dir, old_inode,
  5156. new_dentry->d_name.name,
  5157. new_dentry->d_name.len, 0, index);
  5158. BUG_ON(ret);
  5159. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  5160. btrfs_log_new_name(trans, old_inode, old_dir,
  5161. new_dentry->d_parent);
  5162. btrfs_end_log_trans(root);
  5163. }
  5164. out_fail:
  5165. btrfs_end_transaction_throttle(trans, root);
  5166. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  5167. up_read(&root->fs_info->subvol_sem);
  5168. return ret;
  5169. }
  5170. /*
  5171. * some fairly slow code that needs optimization. This walks the list
  5172. * of all the inodes with pending delalloc and forces them to disk.
  5173. */
  5174. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  5175. {
  5176. struct list_head *head = &root->fs_info->delalloc_inodes;
  5177. struct btrfs_inode *binode;
  5178. struct inode *inode;
  5179. if (root->fs_info->sb->s_flags & MS_RDONLY)
  5180. return -EROFS;
  5181. spin_lock(&root->fs_info->delalloc_lock);
  5182. while (!list_empty(head)) {
  5183. binode = list_entry(head->next, struct btrfs_inode,
  5184. delalloc_inodes);
  5185. inode = igrab(&binode->vfs_inode);
  5186. if (!inode)
  5187. list_del_init(&binode->delalloc_inodes);
  5188. spin_unlock(&root->fs_info->delalloc_lock);
  5189. if (inode) {
  5190. filemap_flush(inode->i_mapping);
  5191. if (delay_iput)
  5192. btrfs_add_delayed_iput(inode);
  5193. else
  5194. iput(inode);
  5195. }
  5196. cond_resched();
  5197. spin_lock(&root->fs_info->delalloc_lock);
  5198. }
  5199. spin_unlock(&root->fs_info->delalloc_lock);
  5200. /* the filemap_flush will queue IO into the worker threads, but
  5201. * we have to make sure the IO is actually started and that
  5202. * ordered extents get created before we return
  5203. */
  5204. atomic_inc(&root->fs_info->async_submit_draining);
  5205. while (atomic_read(&root->fs_info->nr_async_submits) ||
  5206. atomic_read(&root->fs_info->async_delalloc_pages)) {
  5207. wait_event(root->fs_info->async_submit_wait,
  5208. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  5209. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  5210. }
  5211. atomic_dec(&root->fs_info->async_submit_draining);
  5212. return 0;
  5213. }
  5214. int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
  5215. {
  5216. struct btrfs_inode *binode;
  5217. struct inode *inode = NULL;
  5218. spin_lock(&root->fs_info->delalloc_lock);
  5219. while (!list_empty(&root->fs_info->delalloc_inodes)) {
  5220. binode = list_entry(root->fs_info->delalloc_inodes.next,
  5221. struct btrfs_inode, delalloc_inodes);
  5222. inode = igrab(&binode->vfs_inode);
  5223. if (inode) {
  5224. list_move_tail(&binode->delalloc_inodes,
  5225. &root->fs_info->delalloc_inodes);
  5226. break;
  5227. }
  5228. list_del_init(&binode->delalloc_inodes);
  5229. cond_resched_lock(&root->fs_info->delalloc_lock);
  5230. }
  5231. spin_unlock(&root->fs_info->delalloc_lock);
  5232. if (inode) {
  5233. write_inode_now(inode, 0);
  5234. if (delay_iput)
  5235. btrfs_add_delayed_iput(inode);
  5236. else
  5237. iput(inode);
  5238. return 1;
  5239. }
  5240. return 0;
  5241. }
  5242. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  5243. const char *symname)
  5244. {
  5245. struct btrfs_trans_handle *trans;
  5246. struct btrfs_root *root = BTRFS_I(dir)->root;
  5247. struct btrfs_path *path;
  5248. struct btrfs_key key;
  5249. struct inode *inode = NULL;
  5250. int err;
  5251. int drop_inode = 0;
  5252. u64 objectid;
  5253. u64 index = 0 ;
  5254. int name_len;
  5255. int datasize;
  5256. unsigned long ptr;
  5257. struct btrfs_file_extent_item *ei;
  5258. struct extent_buffer *leaf;
  5259. unsigned long nr = 0;
  5260. name_len = strlen(symname) + 1;
  5261. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  5262. return -ENAMETOOLONG;
  5263. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  5264. if (err)
  5265. return err;
  5266. /*
  5267. * 2 items for inode item and ref
  5268. * 2 items for dir items
  5269. * 1 item for xattr if selinux is on
  5270. */
  5271. trans = btrfs_start_transaction(root, 5);
  5272. if (IS_ERR(trans))
  5273. return PTR_ERR(trans);
  5274. btrfs_set_trans_block_group(trans, dir);
  5275. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5276. dentry->d_name.len,
  5277. dentry->d_parent->d_inode->i_ino, objectid,
  5278. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  5279. &index);
  5280. err = PTR_ERR(inode);
  5281. if (IS_ERR(inode))
  5282. goto out_unlock;
  5283. err = btrfs_init_inode_security(trans, inode, dir);
  5284. if (err) {
  5285. drop_inode = 1;
  5286. goto out_unlock;
  5287. }
  5288. btrfs_set_trans_block_group(trans, inode);
  5289. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  5290. if (err)
  5291. drop_inode = 1;
  5292. else {
  5293. inode->i_mapping->a_ops = &btrfs_aops;
  5294. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5295. inode->i_fop = &btrfs_file_operations;
  5296. inode->i_op = &btrfs_file_inode_operations;
  5297. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5298. }
  5299. btrfs_update_inode_block_group(trans, inode);
  5300. btrfs_update_inode_block_group(trans, dir);
  5301. if (drop_inode)
  5302. goto out_unlock;
  5303. path = btrfs_alloc_path();
  5304. BUG_ON(!path);
  5305. key.objectid = inode->i_ino;
  5306. key.offset = 0;
  5307. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  5308. datasize = btrfs_file_extent_calc_inline_size(name_len);
  5309. err = btrfs_insert_empty_item(trans, root, path, &key,
  5310. datasize);
  5311. if (err) {
  5312. drop_inode = 1;
  5313. goto out_unlock;
  5314. }
  5315. leaf = path->nodes[0];
  5316. ei = btrfs_item_ptr(leaf, path->slots[0],
  5317. struct btrfs_file_extent_item);
  5318. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  5319. btrfs_set_file_extent_type(leaf, ei,
  5320. BTRFS_FILE_EXTENT_INLINE);
  5321. btrfs_set_file_extent_encryption(leaf, ei, 0);
  5322. btrfs_set_file_extent_compression(leaf, ei, 0);
  5323. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  5324. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  5325. ptr = btrfs_file_extent_inline_start(ei);
  5326. write_extent_buffer(leaf, symname, ptr, name_len);
  5327. btrfs_mark_buffer_dirty(leaf);
  5328. btrfs_free_path(path);
  5329. inode->i_op = &btrfs_symlink_inode_operations;
  5330. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  5331. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5332. inode_set_bytes(inode, name_len);
  5333. btrfs_i_size_write(inode, name_len - 1);
  5334. err = btrfs_update_inode(trans, root, inode);
  5335. if (err)
  5336. drop_inode = 1;
  5337. out_unlock:
  5338. nr = trans->blocks_used;
  5339. btrfs_end_transaction_throttle(trans, root);
  5340. if (drop_inode) {
  5341. inode_dec_link_count(inode);
  5342. iput(inode);
  5343. }
  5344. btrfs_btree_balance_dirty(root, nr);
  5345. return err;
  5346. }
  5347. static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
  5348. u64 alloc_hint, int mode, loff_t actual_len)
  5349. {
  5350. struct btrfs_trans_handle *trans;
  5351. struct btrfs_root *root = BTRFS_I(inode)->root;
  5352. struct btrfs_key ins;
  5353. u64 cur_offset = start;
  5354. u64 num_bytes = end - start;
  5355. int ret = 0;
  5356. u64 i_size;
  5357. while (num_bytes > 0) {
  5358. trans = btrfs_start_transaction(root, 3);
  5359. if (IS_ERR(trans)) {
  5360. ret = PTR_ERR(trans);
  5361. break;
  5362. }
  5363. ret = btrfs_reserve_extent(trans, root, num_bytes,
  5364. root->sectorsize, 0, alloc_hint,
  5365. (u64)-1, &ins, 1);
  5366. if (ret) {
  5367. btrfs_end_transaction(trans, root);
  5368. break;
  5369. }
  5370. ret = insert_reserved_file_extent(trans, inode,
  5371. cur_offset, ins.objectid,
  5372. ins.offset, ins.offset,
  5373. ins.offset, 0, 0, 0,
  5374. BTRFS_FILE_EXTENT_PREALLOC);
  5375. BUG_ON(ret);
  5376. btrfs_drop_extent_cache(inode, cur_offset,
  5377. cur_offset + ins.offset -1, 0);
  5378. num_bytes -= ins.offset;
  5379. cur_offset += ins.offset;
  5380. alloc_hint = ins.objectid + ins.offset;
  5381. inode->i_ctime = CURRENT_TIME;
  5382. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  5383. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  5384. (actual_len > inode->i_size) &&
  5385. (cur_offset > inode->i_size)) {
  5386. if (cur_offset > actual_len)
  5387. i_size = actual_len;
  5388. else
  5389. i_size = cur_offset;
  5390. i_size_write(inode, i_size);
  5391. btrfs_ordered_update_i_size(inode, i_size, NULL);
  5392. }
  5393. ret = btrfs_update_inode(trans, root, inode);
  5394. BUG_ON(ret);
  5395. btrfs_end_transaction(trans, root);
  5396. }
  5397. return ret;
  5398. }
  5399. static long btrfs_fallocate(struct inode *inode, int mode,
  5400. loff_t offset, loff_t len)
  5401. {
  5402. struct extent_state *cached_state = NULL;
  5403. u64 cur_offset;
  5404. u64 last_byte;
  5405. u64 alloc_start;
  5406. u64 alloc_end;
  5407. u64 alloc_hint = 0;
  5408. u64 locked_end;
  5409. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  5410. struct extent_map *em;
  5411. int ret;
  5412. alloc_start = offset & ~mask;
  5413. alloc_end = (offset + len + mask) & ~mask;
  5414. /*
  5415. * wait for ordered IO before we have any locks. We'll loop again
  5416. * below with the locks held.
  5417. */
  5418. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  5419. mutex_lock(&inode->i_mutex);
  5420. if (alloc_start > inode->i_size) {
  5421. ret = btrfs_cont_expand(inode, alloc_start);
  5422. if (ret)
  5423. goto out;
  5424. }
  5425. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  5426. if (ret)
  5427. goto out;
  5428. locked_end = alloc_end - 1;
  5429. while (1) {
  5430. struct btrfs_ordered_extent *ordered;
  5431. /* the extent lock is ordered inside the running
  5432. * transaction
  5433. */
  5434. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  5435. locked_end, 0, &cached_state, GFP_NOFS);
  5436. ordered = btrfs_lookup_first_ordered_extent(inode,
  5437. alloc_end - 1);
  5438. if (ordered &&
  5439. ordered->file_offset + ordered->len > alloc_start &&
  5440. ordered->file_offset < alloc_end) {
  5441. btrfs_put_ordered_extent(ordered);
  5442. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  5443. alloc_start, locked_end,
  5444. &cached_state, GFP_NOFS);
  5445. /*
  5446. * we can't wait on the range with the transaction
  5447. * running or with the extent lock held
  5448. */
  5449. btrfs_wait_ordered_range(inode, alloc_start,
  5450. alloc_end - alloc_start);
  5451. } else {
  5452. if (ordered)
  5453. btrfs_put_ordered_extent(ordered);
  5454. break;
  5455. }
  5456. }
  5457. cur_offset = alloc_start;
  5458. while (1) {
  5459. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  5460. alloc_end - cur_offset, 0);
  5461. BUG_ON(IS_ERR(em) || !em);
  5462. last_byte = min(extent_map_end(em), alloc_end);
  5463. last_byte = (last_byte + mask) & ~mask;
  5464. if (em->block_start == EXTENT_MAP_HOLE ||
  5465. (cur_offset >= inode->i_size &&
  5466. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5467. ret = prealloc_file_range(inode,
  5468. cur_offset, last_byte,
  5469. alloc_hint, mode, offset+len);
  5470. if (ret < 0) {
  5471. free_extent_map(em);
  5472. break;
  5473. }
  5474. }
  5475. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  5476. alloc_hint = em->block_start;
  5477. free_extent_map(em);
  5478. cur_offset = last_byte;
  5479. if (cur_offset >= alloc_end) {
  5480. ret = 0;
  5481. break;
  5482. }
  5483. }
  5484. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  5485. &cached_state, GFP_NOFS);
  5486. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  5487. out:
  5488. mutex_unlock(&inode->i_mutex);
  5489. return ret;
  5490. }
  5491. static int btrfs_set_page_dirty(struct page *page)
  5492. {
  5493. return __set_page_dirty_nobuffers(page);
  5494. }
  5495. static int btrfs_permission(struct inode *inode, int mask)
  5496. {
  5497. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  5498. return -EACCES;
  5499. return generic_permission(inode, mask, btrfs_check_acl);
  5500. }
  5501. static const struct inode_operations btrfs_dir_inode_operations = {
  5502. .getattr = btrfs_getattr,
  5503. .lookup = btrfs_lookup,
  5504. .create = btrfs_create,
  5505. .unlink = btrfs_unlink,
  5506. .link = btrfs_link,
  5507. .mkdir = btrfs_mkdir,
  5508. .rmdir = btrfs_rmdir,
  5509. .rename = btrfs_rename,
  5510. .symlink = btrfs_symlink,
  5511. .setattr = btrfs_setattr,
  5512. .mknod = btrfs_mknod,
  5513. .setxattr = btrfs_setxattr,
  5514. .getxattr = btrfs_getxattr,
  5515. .listxattr = btrfs_listxattr,
  5516. .removexattr = btrfs_removexattr,
  5517. .permission = btrfs_permission,
  5518. };
  5519. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  5520. .lookup = btrfs_lookup,
  5521. .permission = btrfs_permission,
  5522. };
  5523. static const struct file_operations btrfs_dir_file_operations = {
  5524. .llseek = generic_file_llseek,
  5525. .read = generic_read_dir,
  5526. .readdir = btrfs_real_readdir,
  5527. .unlocked_ioctl = btrfs_ioctl,
  5528. #ifdef CONFIG_COMPAT
  5529. .compat_ioctl = btrfs_ioctl,
  5530. #endif
  5531. .release = btrfs_release_file,
  5532. .fsync = btrfs_sync_file,
  5533. };
  5534. static struct extent_io_ops btrfs_extent_io_ops = {
  5535. .fill_delalloc = run_delalloc_range,
  5536. .submit_bio_hook = btrfs_submit_bio_hook,
  5537. .merge_bio_hook = btrfs_merge_bio_hook,
  5538. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  5539. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  5540. .writepage_start_hook = btrfs_writepage_start_hook,
  5541. .readpage_io_failed_hook = btrfs_io_failed_hook,
  5542. .set_bit_hook = btrfs_set_bit_hook,
  5543. .clear_bit_hook = btrfs_clear_bit_hook,
  5544. .merge_extent_hook = btrfs_merge_extent_hook,
  5545. .split_extent_hook = btrfs_split_extent_hook,
  5546. };
  5547. /*
  5548. * btrfs doesn't support the bmap operation because swapfiles
  5549. * use bmap to make a mapping of extents in the file. They assume
  5550. * these extents won't change over the life of the file and they
  5551. * use the bmap result to do IO directly to the drive.
  5552. *
  5553. * the btrfs bmap call would return logical addresses that aren't
  5554. * suitable for IO and they also will change frequently as COW
  5555. * operations happen. So, swapfile + btrfs == corruption.
  5556. *
  5557. * For now we're avoiding this by dropping bmap.
  5558. */
  5559. static const struct address_space_operations btrfs_aops = {
  5560. .readpage = btrfs_readpage,
  5561. .writepage = btrfs_writepage,
  5562. .writepages = btrfs_writepages,
  5563. .readpages = btrfs_readpages,
  5564. .sync_page = block_sync_page,
  5565. .direct_IO = btrfs_direct_IO,
  5566. .invalidatepage = btrfs_invalidatepage,
  5567. .releasepage = btrfs_releasepage,
  5568. .set_page_dirty = btrfs_set_page_dirty,
  5569. .error_remove_page = generic_error_remove_page,
  5570. };
  5571. static const struct address_space_operations btrfs_symlink_aops = {
  5572. .readpage = btrfs_readpage,
  5573. .writepage = btrfs_writepage,
  5574. .invalidatepage = btrfs_invalidatepage,
  5575. .releasepage = btrfs_releasepage,
  5576. };
  5577. static const struct inode_operations btrfs_file_inode_operations = {
  5578. .truncate = btrfs_truncate,
  5579. .getattr = btrfs_getattr,
  5580. .setattr = btrfs_setattr,
  5581. .setxattr = btrfs_setxattr,
  5582. .getxattr = btrfs_getxattr,
  5583. .listxattr = btrfs_listxattr,
  5584. .removexattr = btrfs_removexattr,
  5585. .permission = btrfs_permission,
  5586. .fallocate = btrfs_fallocate,
  5587. .fiemap = btrfs_fiemap,
  5588. };
  5589. static const struct inode_operations btrfs_special_inode_operations = {
  5590. .getattr = btrfs_getattr,
  5591. .setattr = btrfs_setattr,
  5592. .permission = btrfs_permission,
  5593. .setxattr = btrfs_setxattr,
  5594. .getxattr = btrfs_getxattr,
  5595. .listxattr = btrfs_listxattr,
  5596. .removexattr = btrfs_removexattr,
  5597. };
  5598. static const struct inode_operations btrfs_symlink_inode_operations = {
  5599. .readlink = generic_readlink,
  5600. .follow_link = page_follow_link_light,
  5601. .put_link = page_put_link,
  5602. .permission = btrfs_permission,
  5603. .setxattr = btrfs_setxattr,
  5604. .getxattr = btrfs_getxattr,
  5605. .listxattr = btrfs_listxattr,
  5606. .removexattr = btrfs_removexattr,
  5607. };
  5608. const struct dentry_operations btrfs_dentry_operations = {
  5609. .d_delete = btrfs_dentry_delete,
  5610. };