inode.c 163 KB

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