inode.c 161 KB

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