inode.c 159 KB

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