inode.c 157 KB

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