inode.c 157 KB

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