inode.c 159 KB

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