ioctl.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673
  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/fsnotify.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/highmem.h>
  26. #include <linux/time.h>
  27. #include <linux/init.h>
  28. #include <linux/string.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mount.h>
  31. #include <linux/mpage.h>
  32. #include <linux/namei.h>
  33. #include <linux/swap.h>
  34. #include <linux/writeback.h>
  35. #include <linux/statfs.h>
  36. #include <linux/compat.h>
  37. #include <linux/bit_spinlock.h>
  38. #include <linux/security.h>
  39. #include <linux/xattr.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/slab.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/uuid.h>
  44. #include <linux/btrfs.h>
  45. #include <linux/uaccess.h>
  46. #include "ctree.h"
  47. #include "disk-io.h"
  48. #include "transaction.h"
  49. #include "btrfs_inode.h"
  50. #include "print-tree.h"
  51. #include "volumes.h"
  52. #include "locking.h"
  53. #include "inode-map.h"
  54. #include "backref.h"
  55. #include "rcu-string.h"
  56. #include "send.h"
  57. #include "dev-replace.h"
  58. #include "props.h"
  59. #include "sysfs.h"
  60. #include "qgroup.h"
  61. #include "tree-log.h"
  62. #include "compression.h"
  63. #ifdef CONFIG_64BIT
  64. /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
  65. * structures are incorrect, as the timespec structure from userspace
  66. * is 4 bytes too small. We define these alternatives here to teach
  67. * the kernel about the 32-bit struct packing.
  68. */
  69. struct btrfs_ioctl_timespec_32 {
  70. __u64 sec;
  71. __u32 nsec;
  72. } __attribute__ ((__packed__));
  73. struct btrfs_ioctl_received_subvol_args_32 {
  74. char uuid[BTRFS_UUID_SIZE]; /* in */
  75. __u64 stransid; /* in */
  76. __u64 rtransid; /* out */
  77. struct btrfs_ioctl_timespec_32 stime; /* in */
  78. struct btrfs_ioctl_timespec_32 rtime; /* out */
  79. __u64 flags; /* in */
  80. __u64 reserved[16]; /* in */
  81. } __attribute__ ((__packed__));
  82. #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
  83. struct btrfs_ioctl_received_subvol_args_32)
  84. #endif
  85. static int btrfs_clone(struct inode *src, struct inode *inode,
  86. u64 off, u64 olen, u64 olen_aligned, u64 destoff,
  87. int no_time_update);
  88. /* Mask out flags that are inappropriate for the given type of inode. */
  89. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  90. {
  91. if (S_ISDIR(mode))
  92. return flags;
  93. else if (S_ISREG(mode))
  94. return flags & ~FS_DIRSYNC_FL;
  95. else
  96. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  97. }
  98. /*
  99. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  100. */
  101. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  102. {
  103. unsigned int iflags = 0;
  104. if (flags & BTRFS_INODE_SYNC)
  105. iflags |= FS_SYNC_FL;
  106. if (flags & BTRFS_INODE_IMMUTABLE)
  107. iflags |= FS_IMMUTABLE_FL;
  108. if (flags & BTRFS_INODE_APPEND)
  109. iflags |= FS_APPEND_FL;
  110. if (flags & BTRFS_INODE_NODUMP)
  111. iflags |= FS_NODUMP_FL;
  112. if (flags & BTRFS_INODE_NOATIME)
  113. iflags |= FS_NOATIME_FL;
  114. if (flags & BTRFS_INODE_DIRSYNC)
  115. iflags |= FS_DIRSYNC_FL;
  116. if (flags & BTRFS_INODE_NODATACOW)
  117. iflags |= FS_NOCOW_FL;
  118. if (flags & BTRFS_INODE_NOCOMPRESS)
  119. iflags |= FS_NOCOMP_FL;
  120. else if (flags & BTRFS_INODE_COMPRESS)
  121. iflags |= FS_COMPR_FL;
  122. return iflags;
  123. }
  124. /*
  125. * Update inode->i_flags based on the btrfs internal flags.
  126. */
  127. void btrfs_update_iflags(struct inode *inode)
  128. {
  129. struct btrfs_inode *ip = BTRFS_I(inode);
  130. unsigned int new_fl = 0;
  131. if (ip->flags & BTRFS_INODE_SYNC)
  132. new_fl |= S_SYNC;
  133. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  134. new_fl |= S_IMMUTABLE;
  135. if (ip->flags & BTRFS_INODE_APPEND)
  136. new_fl |= S_APPEND;
  137. if (ip->flags & BTRFS_INODE_NOATIME)
  138. new_fl |= S_NOATIME;
  139. if (ip->flags & BTRFS_INODE_DIRSYNC)
  140. new_fl |= S_DIRSYNC;
  141. set_mask_bits(&inode->i_flags,
  142. S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
  143. new_fl);
  144. }
  145. /*
  146. * Inherit flags from the parent inode.
  147. *
  148. * Currently only the compression flags and the cow flags are inherited.
  149. */
  150. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  151. {
  152. unsigned int flags;
  153. if (!dir)
  154. return;
  155. flags = BTRFS_I(dir)->flags;
  156. if (flags & BTRFS_INODE_NOCOMPRESS) {
  157. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  158. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  159. } else if (flags & BTRFS_INODE_COMPRESS) {
  160. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  161. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  162. }
  163. if (flags & BTRFS_INODE_NODATACOW) {
  164. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  165. if (S_ISREG(inode->i_mode))
  166. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  167. }
  168. btrfs_update_iflags(inode);
  169. }
  170. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  171. {
  172. struct btrfs_inode *ip = BTRFS_I(file_inode(file));
  173. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  174. if (copy_to_user(arg, &flags, sizeof(flags)))
  175. return -EFAULT;
  176. return 0;
  177. }
  178. static int check_flags(unsigned int flags)
  179. {
  180. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  181. FS_NOATIME_FL | FS_NODUMP_FL | \
  182. FS_SYNC_FL | FS_DIRSYNC_FL | \
  183. FS_NOCOMP_FL | FS_COMPR_FL |
  184. FS_NOCOW_FL))
  185. return -EOPNOTSUPP;
  186. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  187. return -EINVAL;
  188. return 0;
  189. }
  190. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  191. {
  192. struct inode *inode = file_inode(file);
  193. struct btrfs_inode *ip = BTRFS_I(inode);
  194. struct btrfs_root *root = ip->root;
  195. struct btrfs_trans_handle *trans;
  196. unsigned int flags, oldflags;
  197. int ret;
  198. u64 ip_oldflags;
  199. unsigned int i_oldflags;
  200. umode_t mode;
  201. if (!inode_owner_or_capable(inode))
  202. return -EPERM;
  203. if (btrfs_root_readonly(root))
  204. return -EROFS;
  205. if (copy_from_user(&flags, arg, sizeof(flags)))
  206. return -EFAULT;
  207. ret = check_flags(flags);
  208. if (ret)
  209. return ret;
  210. ret = mnt_want_write_file(file);
  211. if (ret)
  212. return ret;
  213. inode_lock(inode);
  214. ip_oldflags = ip->flags;
  215. i_oldflags = inode->i_flags;
  216. mode = inode->i_mode;
  217. flags = btrfs_mask_flags(inode->i_mode, flags);
  218. oldflags = btrfs_flags_to_ioctl(ip->flags);
  219. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  220. if (!capable(CAP_LINUX_IMMUTABLE)) {
  221. ret = -EPERM;
  222. goto out_unlock;
  223. }
  224. }
  225. if (flags & FS_SYNC_FL)
  226. ip->flags |= BTRFS_INODE_SYNC;
  227. else
  228. ip->flags &= ~BTRFS_INODE_SYNC;
  229. if (flags & FS_IMMUTABLE_FL)
  230. ip->flags |= BTRFS_INODE_IMMUTABLE;
  231. else
  232. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  233. if (flags & FS_APPEND_FL)
  234. ip->flags |= BTRFS_INODE_APPEND;
  235. else
  236. ip->flags &= ~BTRFS_INODE_APPEND;
  237. if (flags & FS_NODUMP_FL)
  238. ip->flags |= BTRFS_INODE_NODUMP;
  239. else
  240. ip->flags &= ~BTRFS_INODE_NODUMP;
  241. if (flags & FS_NOATIME_FL)
  242. ip->flags |= BTRFS_INODE_NOATIME;
  243. else
  244. ip->flags &= ~BTRFS_INODE_NOATIME;
  245. if (flags & FS_DIRSYNC_FL)
  246. ip->flags |= BTRFS_INODE_DIRSYNC;
  247. else
  248. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  249. if (flags & FS_NOCOW_FL) {
  250. if (S_ISREG(mode)) {
  251. /*
  252. * It's safe to turn csums off here, no extents exist.
  253. * Otherwise we want the flag to reflect the real COW
  254. * status of the file and will not set it.
  255. */
  256. if (inode->i_size == 0)
  257. ip->flags |= BTRFS_INODE_NODATACOW
  258. | BTRFS_INODE_NODATASUM;
  259. } else {
  260. ip->flags |= BTRFS_INODE_NODATACOW;
  261. }
  262. } else {
  263. /*
  264. * Revert back under same assumptions as above
  265. */
  266. if (S_ISREG(mode)) {
  267. if (inode->i_size == 0)
  268. ip->flags &= ~(BTRFS_INODE_NODATACOW
  269. | BTRFS_INODE_NODATASUM);
  270. } else {
  271. ip->flags &= ~BTRFS_INODE_NODATACOW;
  272. }
  273. }
  274. /*
  275. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  276. * flag may be changed automatically if compression code won't make
  277. * things smaller.
  278. */
  279. if (flags & FS_NOCOMP_FL) {
  280. ip->flags &= ~BTRFS_INODE_COMPRESS;
  281. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  282. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  283. if (ret && ret != -ENODATA)
  284. goto out_drop;
  285. } else if (flags & FS_COMPR_FL) {
  286. const char *comp;
  287. ip->flags |= BTRFS_INODE_COMPRESS;
  288. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  289. if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
  290. comp = "lzo";
  291. else
  292. comp = "zlib";
  293. ret = btrfs_set_prop(inode, "btrfs.compression",
  294. comp, strlen(comp), 0);
  295. if (ret)
  296. goto out_drop;
  297. } else {
  298. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  299. if (ret && ret != -ENODATA)
  300. goto out_drop;
  301. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  302. }
  303. trans = btrfs_start_transaction(root, 1);
  304. if (IS_ERR(trans)) {
  305. ret = PTR_ERR(trans);
  306. goto out_drop;
  307. }
  308. btrfs_update_iflags(inode);
  309. inode_inc_iversion(inode);
  310. inode->i_ctime = current_time(inode);
  311. ret = btrfs_update_inode(trans, root, inode);
  312. btrfs_end_transaction(trans, root);
  313. out_drop:
  314. if (ret) {
  315. ip->flags = ip_oldflags;
  316. inode->i_flags = i_oldflags;
  317. }
  318. out_unlock:
  319. inode_unlock(inode);
  320. mnt_drop_write_file(file);
  321. return ret;
  322. }
  323. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  324. {
  325. struct inode *inode = file_inode(file);
  326. return put_user(inode->i_generation, arg);
  327. }
  328. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  329. {
  330. struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
  331. struct btrfs_device *device;
  332. struct request_queue *q;
  333. struct fstrim_range range;
  334. u64 minlen = ULLONG_MAX;
  335. u64 num_devices = 0;
  336. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  337. int ret;
  338. if (!capable(CAP_SYS_ADMIN))
  339. return -EPERM;
  340. rcu_read_lock();
  341. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  342. dev_list) {
  343. if (!device->bdev)
  344. continue;
  345. q = bdev_get_queue(device->bdev);
  346. if (blk_queue_discard(q)) {
  347. num_devices++;
  348. minlen = min((u64)q->limits.discard_granularity,
  349. minlen);
  350. }
  351. }
  352. rcu_read_unlock();
  353. if (!num_devices)
  354. return -EOPNOTSUPP;
  355. if (copy_from_user(&range, arg, sizeof(range)))
  356. return -EFAULT;
  357. if (range.start > total_bytes ||
  358. range.len < fs_info->sb->s_blocksize)
  359. return -EINVAL;
  360. range.len = min(range.len, total_bytes - range.start);
  361. range.minlen = max(range.minlen, minlen);
  362. ret = btrfs_trim_fs(fs_info->tree_root, &range);
  363. if (ret < 0)
  364. return ret;
  365. if (copy_to_user(arg, &range, sizeof(range)))
  366. return -EFAULT;
  367. return 0;
  368. }
  369. int btrfs_is_empty_uuid(u8 *uuid)
  370. {
  371. int i;
  372. for (i = 0; i < BTRFS_UUID_SIZE; i++) {
  373. if (uuid[i])
  374. return 0;
  375. }
  376. return 1;
  377. }
  378. static noinline int create_subvol(struct inode *dir,
  379. struct dentry *dentry,
  380. char *name, int namelen,
  381. u64 *async_transid,
  382. struct btrfs_qgroup_inherit *inherit)
  383. {
  384. struct btrfs_trans_handle *trans;
  385. struct btrfs_key key;
  386. struct btrfs_root_item *root_item;
  387. struct btrfs_inode_item *inode_item;
  388. struct extent_buffer *leaf;
  389. struct btrfs_root *root = BTRFS_I(dir)->root;
  390. struct btrfs_root *new_root;
  391. struct btrfs_block_rsv block_rsv;
  392. struct timespec cur_time = current_time(dir);
  393. struct inode *inode;
  394. int ret;
  395. int err;
  396. u64 objectid;
  397. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  398. u64 index = 0;
  399. u64 qgroup_reserved;
  400. uuid_le new_uuid;
  401. root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
  402. if (!root_item)
  403. return -ENOMEM;
  404. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  405. if (ret)
  406. goto fail_free;
  407. /*
  408. * Don't create subvolume whose level is not zero. Or qgroup will be
  409. * screwed up since it assumes subvolume qgroup's level to be 0.
  410. */
  411. if (btrfs_qgroup_level(objectid)) {
  412. ret = -ENOSPC;
  413. goto fail_free;
  414. }
  415. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  416. /*
  417. * The same as the snapshot creation, please see the comment
  418. * of create_snapshot().
  419. */
  420. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  421. 8, &qgroup_reserved, false);
  422. if (ret)
  423. goto fail_free;
  424. trans = btrfs_start_transaction(root, 0);
  425. if (IS_ERR(trans)) {
  426. ret = PTR_ERR(trans);
  427. btrfs_subvolume_release_metadata(root, &block_rsv,
  428. qgroup_reserved);
  429. goto fail_free;
  430. }
  431. trans->block_rsv = &block_rsv;
  432. trans->bytes_reserved = block_rsv.size;
  433. ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
  434. if (ret)
  435. goto fail;
  436. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  437. if (IS_ERR(leaf)) {
  438. ret = PTR_ERR(leaf);
  439. goto fail;
  440. }
  441. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  442. btrfs_set_header_bytenr(leaf, leaf->start);
  443. btrfs_set_header_generation(leaf, trans->transid);
  444. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  445. btrfs_set_header_owner(leaf, objectid);
  446. write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
  447. BTRFS_FSID_SIZE);
  448. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  449. btrfs_header_chunk_tree_uuid(leaf),
  450. BTRFS_UUID_SIZE);
  451. btrfs_mark_buffer_dirty(leaf);
  452. inode_item = &root_item->inode;
  453. btrfs_set_stack_inode_generation(inode_item, 1);
  454. btrfs_set_stack_inode_size(inode_item, 3);
  455. btrfs_set_stack_inode_nlink(inode_item, 1);
  456. btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
  457. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  458. btrfs_set_root_flags(root_item, 0);
  459. btrfs_set_root_limit(root_item, 0);
  460. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  461. btrfs_set_root_bytenr(root_item, leaf->start);
  462. btrfs_set_root_generation(root_item, trans->transid);
  463. btrfs_set_root_level(root_item, 0);
  464. btrfs_set_root_refs(root_item, 1);
  465. btrfs_set_root_used(root_item, leaf->len);
  466. btrfs_set_root_last_snapshot(root_item, 0);
  467. btrfs_set_root_generation_v2(root_item,
  468. btrfs_root_generation(root_item));
  469. uuid_le_gen(&new_uuid);
  470. memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  471. btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
  472. btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
  473. root_item->ctime = root_item->otime;
  474. btrfs_set_root_ctransid(root_item, trans->transid);
  475. btrfs_set_root_otransid(root_item, trans->transid);
  476. btrfs_tree_unlock(leaf);
  477. free_extent_buffer(leaf);
  478. leaf = NULL;
  479. btrfs_set_root_dirid(root_item, new_dirid);
  480. key.objectid = objectid;
  481. key.offset = 0;
  482. key.type = BTRFS_ROOT_ITEM_KEY;
  483. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  484. root_item);
  485. if (ret)
  486. goto fail;
  487. key.offset = (u64)-1;
  488. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  489. if (IS_ERR(new_root)) {
  490. ret = PTR_ERR(new_root);
  491. btrfs_abort_transaction(trans, ret);
  492. goto fail;
  493. }
  494. btrfs_record_root_in_trans(trans, new_root);
  495. ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
  496. if (ret) {
  497. /* We potentially lose an unused inode item here */
  498. btrfs_abort_transaction(trans, ret);
  499. goto fail;
  500. }
  501. mutex_lock(&new_root->objectid_mutex);
  502. new_root->highest_objectid = new_dirid;
  503. mutex_unlock(&new_root->objectid_mutex);
  504. /*
  505. * insert the directory item
  506. */
  507. ret = btrfs_set_inode_index(dir, &index);
  508. if (ret) {
  509. btrfs_abort_transaction(trans, ret);
  510. goto fail;
  511. }
  512. ret = btrfs_insert_dir_item(trans, root,
  513. name, namelen, dir, &key,
  514. BTRFS_FT_DIR, index);
  515. if (ret) {
  516. btrfs_abort_transaction(trans, ret);
  517. goto fail;
  518. }
  519. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  520. ret = btrfs_update_inode(trans, root, dir);
  521. BUG_ON(ret);
  522. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  523. objectid, root->root_key.objectid,
  524. btrfs_ino(dir), index, name, namelen);
  525. BUG_ON(ret);
  526. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  527. root_item->uuid, BTRFS_UUID_KEY_SUBVOL,
  528. objectid);
  529. if (ret)
  530. btrfs_abort_transaction(trans, ret);
  531. fail:
  532. kfree(root_item);
  533. trans->block_rsv = NULL;
  534. trans->bytes_reserved = 0;
  535. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  536. if (async_transid) {
  537. *async_transid = trans->transid;
  538. err = btrfs_commit_transaction_async(trans, root, 1);
  539. if (err)
  540. err = btrfs_commit_transaction(trans, root);
  541. } else {
  542. err = btrfs_commit_transaction(trans, root);
  543. }
  544. if (err && !ret)
  545. ret = err;
  546. if (!ret) {
  547. inode = btrfs_lookup_dentry(dir, dentry);
  548. if (IS_ERR(inode))
  549. return PTR_ERR(inode);
  550. d_instantiate(dentry, inode);
  551. }
  552. return ret;
  553. fail_free:
  554. kfree(root_item);
  555. return ret;
  556. }
  557. static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
  558. {
  559. s64 writers;
  560. DEFINE_WAIT(wait);
  561. do {
  562. prepare_to_wait(&root->subv_writers->wait, &wait,
  563. TASK_UNINTERRUPTIBLE);
  564. writers = percpu_counter_sum(&root->subv_writers->counter);
  565. if (writers)
  566. schedule();
  567. finish_wait(&root->subv_writers->wait, &wait);
  568. } while (writers);
  569. }
  570. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  571. struct dentry *dentry, char *name, int namelen,
  572. u64 *async_transid, bool readonly,
  573. struct btrfs_qgroup_inherit *inherit)
  574. {
  575. struct inode *inode;
  576. struct btrfs_pending_snapshot *pending_snapshot;
  577. struct btrfs_trans_handle *trans;
  578. int ret;
  579. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  580. return -EINVAL;
  581. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  582. if (!pending_snapshot)
  583. return -ENOMEM;
  584. pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
  585. GFP_NOFS);
  586. pending_snapshot->path = btrfs_alloc_path();
  587. if (!pending_snapshot->root_item || !pending_snapshot->path) {
  588. ret = -ENOMEM;
  589. goto free_pending;
  590. }
  591. atomic_inc(&root->will_be_snapshoted);
  592. smp_mb__after_atomic();
  593. btrfs_wait_for_no_snapshoting_writes(root);
  594. ret = btrfs_start_delalloc_inodes(root, 0);
  595. if (ret)
  596. goto dec_and_free;
  597. btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
  598. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  599. BTRFS_BLOCK_RSV_TEMP);
  600. /*
  601. * 1 - parent dir inode
  602. * 2 - dir entries
  603. * 1 - root item
  604. * 2 - root ref/backref
  605. * 1 - root of snapshot
  606. * 1 - UUID item
  607. */
  608. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
  609. &pending_snapshot->block_rsv, 8,
  610. &pending_snapshot->qgroup_reserved,
  611. false);
  612. if (ret)
  613. goto dec_and_free;
  614. pending_snapshot->dentry = dentry;
  615. pending_snapshot->root = root;
  616. pending_snapshot->readonly = readonly;
  617. pending_snapshot->dir = dir;
  618. pending_snapshot->inherit = inherit;
  619. trans = btrfs_start_transaction(root, 0);
  620. if (IS_ERR(trans)) {
  621. ret = PTR_ERR(trans);
  622. goto fail;
  623. }
  624. spin_lock(&root->fs_info->trans_lock);
  625. list_add(&pending_snapshot->list,
  626. &trans->transaction->pending_snapshots);
  627. spin_unlock(&root->fs_info->trans_lock);
  628. if (async_transid) {
  629. *async_transid = trans->transid;
  630. ret = btrfs_commit_transaction_async(trans,
  631. root->fs_info->extent_root, 1);
  632. if (ret)
  633. ret = btrfs_commit_transaction(trans, root);
  634. } else {
  635. ret = btrfs_commit_transaction(trans,
  636. root->fs_info->extent_root);
  637. }
  638. if (ret)
  639. goto fail;
  640. ret = pending_snapshot->error;
  641. if (ret)
  642. goto fail;
  643. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  644. if (ret)
  645. goto fail;
  646. inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
  647. if (IS_ERR(inode)) {
  648. ret = PTR_ERR(inode);
  649. goto fail;
  650. }
  651. d_instantiate(dentry, inode);
  652. ret = 0;
  653. fail:
  654. btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
  655. &pending_snapshot->block_rsv,
  656. pending_snapshot->qgroup_reserved);
  657. dec_and_free:
  658. if (atomic_dec_and_test(&root->will_be_snapshoted))
  659. wake_up_atomic_t(&root->will_be_snapshoted);
  660. free_pending:
  661. kfree(pending_snapshot->root_item);
  662. btrfs_free_path(pending_snapshot->path);
  663. kfree(pending_snapshot);
  664. return ret;
  665. }
  666. /* copy of may_delete in fs/namei.c()
  667. * Check whether we can remove a link victim from directory dir, check
  668. * whether the type of victim is right.
  669. * 1. We can't do it if dir is read-only (done in permission())
  670. * 2. We should have write and exec permissions on dir
  671. * 3. We can't remove anything from append-only dir
  672. * 4. We can't do anything with immutable dir (done in permission())
  673. * 5. If the sticky bit on dir is set we should either
  674. * a. be owner of dir, or
  675. * b. be owner of victim, or
  676. * c. have CAP_FOWNER capability
  677. * 6. If the victim is append-only or immutable we can't do anything with
  678. * links pointing to it.
  679. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  680. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  681. * 9. We can't remove a root or mountpoint.
  682. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  683. * nfs_async_unlink().
  684. */
  685. static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
  686. {
  687. int error;
  688. if (d_really_is_negative(victim))
  689. return -ENOENT;
  690. BUG_ON(d_inode(victim->d_parent) != dir);
  691. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  692. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  693. if (error)
  694. return error;
  695. if (IS_APPEND(dir))
  696. return -EPERM;
  697. if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
  698. IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
  699. return -EPERM;
  700. if (isdir) {
  701. if (!d_is_dir(victim))
  702. return -ENOTDIR;
  703. if (IS_ROOT(victim))
  704. return -EBUSY;
  705. } else if (d_is_dir(victim))
  706. return -EISDIR;
  707. if (IS_DEADDIR(dir))
  708. return -ENOENT;
  709. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  710. return -EBUSY;
  711. return 0;
  712. }
  713. /* copy of may_create in fs/namei.c() */
  714. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  715. {
  716. if (d_really_is_positive(child))
  717. return -EEXIST;
  718. if (IS_DEADDIR(dir))
  719. return -ENOENT;
  720. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  721. }
  722. /*
  723. * Create a new subvolume below @parent. This is largely modeled after
  724. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  725. * inside this filesystem so it's quite a bit simpler.
  726. */
  727. static noinline int btrfs_mksubvol(struct path *parent,
  728. char *name, int namelen,
  729. struct btrfs_root *snap_src,
  730. u64 *async_transid, bool readonly,
  731. struct btrfs_qgroup_inherit *inherit)
  732. {
  733. struct inode *dir = d_inode(parent->dentry);
  734. struct dentry *dentry;
  735. int error;
  736. error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
  737. if (error == -EINTR)
  738. return error;
  739. dentry = lookup_one_len(name, parent->dentry, namelen);
  740. error = PTR_ERR(dentry);
  741. if (IS_ERR(dentry))
  742. goto out_unlock;
  743. error = btrfs_may_create(dir, dentry);
  744. if (error)
  745. goto out_dput;
  746. /*
  747. * even if this name doesn't exist, we may get hash collisions.
  748. * check for them now when we can safely fail
  749. */
  750. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  751. dir->i_ino, name,
  752. namelen);
  753. if (error)
  754. goto out_dput;
  755. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  756. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  757. goto out_up_read;
  758. if (snap_src) {
  759. error = create_snapshot(snap_src, dir, dentry, name, namelen,
  760. async_transid, readonly, inherit);
  761. } else {
  762. error = create_subvol(dir, dentry, name, namelen,
  763. async_transid, inherit);
  764. }
  765. if (!error)
  766. fsnotify_mkdir(dir, dentry);
  767. out_up_read:
  768. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  769. out_dput:
  770. dput(dentry);
  771. out_unlock:
  772. inode_unlock(dir);
  773. return error;
  774. }
  775. /*
  776. * When we're defragging a range, we don't want to kick it off again
  777. * if it is really just waiting for delalloc to send it down.
  778. * If we find a nice big extent or delalloc range for the bytes in the
  779. * file you want to defrag, we return 0 to let you know to skip this
  780. * part of the file
  781. */
  782. static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
  783. {
  784. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  785. struct extent_map *em = NULL;
  786. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  787. u64 end;
  788. read_lock(&em_tree->lock);
  789. em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
  790. read_unlock(&em_tree->lock);
  791. if (em) {
  792. end = extent_map_end(em);
  793. free_extent_map(em);
  794. if (end - offset > thresh)
  795. return 0;
  796. }
  797. /* if we already have a nice delalloc here, just stop */
  798. thresh /= 2;
  799. end = count_range_bits(io_tree, &offset, offset + thresh,
  800. thresh, EXTENT_DELALLOC, 1);
  801. if (end >= thresh)
  802. return 0;
  803. return 1;
  804. }
  805. /*
  806. * helper function to walk through a file and find extents
  807. * newer than a specific transid, and smaller than thresh.
  808. *
  809. * This is used by the defragging code to find new and small
  810. * extents
  811. */
  812. static int find_new_extents(struct btrfs_root *root,
  813. struct inode *inode, u64 newer_than,
  814. u64 *off, u32 thresh)
  815. {
  816. struct btrfs_path *path;
  817. struct btrfs_key min_key;
  818. struct extent_buffer *leaf;
  819. struct btrfs_file_extent_item *extent;
  820. int type;
  821. int ret;
  822. u64 ino = btrfs_ino(inode);
  823. path = btrfs_alloc_path();
  824. if (!path)
  825. return -ENOMEM;
  826. min_key.objectid = ino;
  827. min_key.type = BTRFS_EXTENT_DATA_KEY;
  828. min_key.offset = *off;
  829. while (1) {
  830. ret = btrfs_search_forward(root, &min_key, path, newer_than);
  831. if (ret != 0)
  832. goto none;
  833. process_slot:
  834. if (min_key.objectid != ino)
  835. goto none;
  836. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  837. goto none;
  838. leaf = path->nodes[0];
  839. extent = btrfs_item_ptr(leaf, path->slots[0],
  840. struct btrfs_file_extent_item);
  841. type = btrfs_file_extent_type(leaf, extent);
  842. if (type == BTRFS_FILE_EXTENT_REG &&
  843. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  844. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  845. *off = min_key.offset;
  846. btrfs_free_path(path);
  847. return 0;
  848. }
  849. path->slots[0]++;
  850. if (path->slots[0] < btrfs_header_nritems(leaf)) {
  851. btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
  852. goto process_slot;
  853. }
  854. if (min_key.offset == (u64)-1)
  855. goto none;
  856. min_key.offset++;
  857. btrfs_release_path(path);
  858. }
  859. none:
  860. btrfs_free_path(path);
  861. return -ENOENT;
  862. }
  863. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  864. {
  865. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  866. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  867. struct extent_map *em;
  868. u64 len = PAGE_SIZE;
  869. /*
  870. * hopefully we have this extent in the tree already, try without
  871. * the full extent lock
  872. */
  873. read_lock(&em_tree->lock);
  874. em = lookup_extent_mapping(em_tree, start, len);
  875. read_unlock(&em_tree->lock);
  876. if (!em) {
  877. struct extent_state *cached = NULL;
  878. u64 end = start + len - 1;
  879. /* get the big lock and read metadata off disk */
  880. lock_extent_bits(io_tree, start, end, &cached);
  881. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  882. unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
  883. if (IS_ERR(em))
  884. return NULL;
  885. }
  886. return em;
  887. }
  888. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  889. {
  890. struct extent_map *next;
  891. bool ret = true;
  892. /* this is the last extent */
  893. if (em->start + em->len >= i_size_read(inode))
  894. return false;
  895. next = defrag_lookup_extent(inode, em->start + em->len);
  896. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  897. ret = false;
  898. else if ((em->block_start + em->block_len == next->block_start) &&
  899. (em->block_len > SZ_128K && next->block_len > SZ_128K))
  900. ret = false;
  901. free_extent_map(next);
  902. return ret;
  903. }
  904. static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
  905. u64 *last_len, u64 *skip, u64 *defrag_end,
  906. int compress)
  907. {
  908. struct extent_map *em;
  909. int ret = 1;
  910. bool next_mergeable = true;
  911. bool prev_mergeable = true;
  912. /*
  913. * make sure that once we start defragging an extent, we keep on
  914. * defragging it
  915. */
  916. if (start < *defrag_end)
  917. return 1;
  918. *skip = 0;
  919. em = defrag_lookup_extent(inode, start);
  920. if (!em)
  921. return 0;
  922. /* this will cover holes, and inline extents */
  923. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  924. ret = 0;
  925. goto out;
  926. }
  927. if (!*defrag_end)
  928. prev_mergeable = false;
  929. next_mergeable = defrag_check_next_extent(inode, em);
  930. /*
  931. * we hit a real extent, if it is big or the next extent is not a
  932. * real extent, don't bother defragging it
  933. */
  934. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  935. (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
  936. ret = 0;
  937. out:
  938. /*
  939. * last_len ends up being a counter of how many bytes we've defragged.
  940. * every time we choose not to defrag an extent, we reset *last_len
  941. * so that the next tiny extent will force a defrag.
  942. *
  943. * The end result of this is that tiny extents before a single big
  944. * extent will force at least part of that big extent to be defragged.
  945. */
  946. if (ret) {
  947. *defrag_end = extent_map_end(em);
  948. } else {
  949. *last_len = 0;
  950. *skip = extent_map_end(em);
  951. *defrag_end = 0;
  952. }
  953. free_extent_map(em);
  954. return ret;
  955. }
  956. /*
  957. * it doesn't do much good to defrag one or two pages
  958. * at a time. This pulls in a nice chunk of pages
  959. * to COW and defrag.
  960. *
  961. * It also makes sure the delalloc code has enough
  962. * dirty data to avoid making new small extents as part
  963. * of the defrag
  964. *
  965. * It's a good idea to start RA on this range
  966. * before calling this.
  967. */
  968. static int cluster_pages_for_defrag(struct inode *inode,
  969. struct page **pages,
  970. unsigned long start_index,
  971. unsigned long num_pages)
  972. {
  973. unsigned long file_end;
  974. u64 isize = i_size_read(inode);
  975. u64 page_start;
  976. u64 page_end;
  977. u64 page_cnt;
  978. int ret;
  979. int i;
  980. int i_done;
  981. struct btrfs_ordered_extent *ordered;
  982. struct extent_state *cached_state = NULL;
  983. struct extent_io_tree *tree;
  984. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  985. file_end = (isize - 1) >> PAGE_SHIFT;
  986. if (!isize || start_index > file_end)
  987. return 0;
  988. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  989. ret = btrfs_delalloc_reserve_space(inode,
  990. start_index << PAGE_SHIFT,
  991. page_cnt << PAGE_SHIFT);
  992. if (ret)
  993. return ret;
  994. i_done = 0;
  995. tree = &BTRFS_I(inode)->io_tree;
  996. /* step one, lock all the pages */
  997. for (i = 0; i < page_cnt; i++) {
  998. struct page *page;
  999. again:
  1000. page = find_or_create_page(inode->i_mapping,
  1001. start_index + i, mask);
  1002. if (!page)
  1003. break;
  1004. page_start = page_offset(page);
  1005. page_end = page_start + PAGE_SIZE - 1;
  1006. while (1) {
  1007. lock_extent_bits(tree, page_start, page_end,
  1008. &cached_state);
  1009. ordered = btrfs_lookup_ordered_extent(inode,
  1010. page_start);
  1011. unlock_extent_cached(tree, page_start, page_end,
  1012. &cached_state, GFP_NOFS);
  1013. if (!ordered)
  1014. break;
  1015. unlock_page(page);
  1016. btrfs_start_ordered_extent(inode, ordered, 1);
  1017. btrfs_put_ordered_extent(ordered);
  1018. lock_page(page);
  1019. /*
  1020. * we unlocked the page above, so we need check if
  1021. * it was released or not.
  1022. */
  1023. if (page->mapping != inode->i_mapping) {
  1024. unlock_page(page);
  1025. put_page(page);
  1026. goto again;
  1027. }
  1028. }
  1029. if (!PageUptodate(page)) {
  1030. btrfs_readpage(NULL, page);
  1031. lock_page(page);
  1032. if (!PageUptodate(page)) {
  1033. unlock_page(page);
  1034. put_page(page);
  1035. ret = -EIO;
  1036. break;
  1037. }
  1038. }
  1039. if (page->mapping != inode->i_mapping) {
  1040. unlock_page(page);
  1041. put_page(page);
  1042. goto again;
  1043. }
  1044. pages[i] = page;
  1045. i_done++;
  1046. }
  1047. if (!i_done || ret)
  1048. goto out;
  1049. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1050. goto out;
  1051. /*
  1052. * so now we have a nice long stream of locked
  1053. * and up to date pages, lets wait on them
  1054. */
  1055. for (i = 0; i < i_done; i++)
  1056. wait_on_page_writeback(pages[i]);
  1057. page_start = page_offset(pages[0]);
  1058. page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
  1059. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1060. page_start, page_end - 1, &cached_state);
  1061. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  1062. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1063. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  1064. &cached_state, GFP_NOFS);
  1065. if (i_done != page_cnt) {
  1066. spin_lock(&BTRFS_I(inode)->lock);
  1067. BTRFS_I(inode)->outstanding_extents++;
  1068. spin_unlock(&BTRFS_I(inode)->lock);
  1069. btrfs_delalloc_release_space(inode,
  1070. start_index << PAGE_SHIFT,
  1071. (page_cnt - i_done) << PAGE_SHIFT);
  1072. }
  1073. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  1074. &cached_state);
  1075. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1076. page_start, page_end - 1, &cached_state,
  1077. GFP_NOFS);
  1078. for (i = 0; i < i_done; i++) {
  1079. clear_page_dirty_for_io(pages[i]);
  1080. ClearPageChecked(pages[i]);
  1081. set_page_extent_mapped(pages[i]);
  1082. set_page_dirty(pages[i]);
  1083. unlock_page(pages[i]);
  1084. put_page(pages[i]);
  1085. }
  1086. return i_done;
  1087. out:
  1088. for (i = 0; i < i_done; i++) {
  1089. unlock_page(pages[i]);
  1090. put_page(pages[i]);
  1091. }
  1092. btrfs_delalloc_release_space(inode,
  1093. start_index << PAGE_SHIFT,
  1094. page_cnt << PAGE_SHIFT);
  1095. return ret;
  1096. }
  1097. int btrfs_defrag_file(struct inode *inode, struct file *file,
  1098. struct btrfs_ioctl_defrag_range_args *range,
  1099. u64 newer_than, unsigned long max_to_defrag)
  1100. {
  1101. struct btrfs_root *root = BTRFS_I(inode)->root;
  1102. struct file_ra_state *ra = NULL;
  1103. unsigned long last_index;
  1104. u64 isize = i_size_read(inode);
  1105. u64 last_len = 0;
  1106. u64 skip = 0;
  1107. u64 defrag_end = 0;
  1108. u64 newer_off = range->start;
  1109. unsigned long i;
  1110. unsigned long ra_index = 0;
  1111. int ret;
  1112. int defrag_count = 0;
  1113. int compress_type = BTRFS_COMPRESS_ZLIB;
  1114. u32 extent_thresh = range->extent_thresh;
  1115. unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
  1116. unsigned long cluster = max_cluster;
  1117. u64 new_align = ~((u64)SZ_128K - 1);
  1118. struct page **pages = NULL;
  1119. if (isize == 0)
  1120. return 0;
  1121. if (range->start >= isize)
  1122. return -EINVAL;
  1123. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1124. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  1125. return -EINVAL;
  1126. if (range->compress_type)
  1127. compress_type = range->compress_type;
  1128. }
  1129. if (extent_thresh == 0)
  1130. extent_thresh = SZ_256K;
  1131. /*
  1132. * if we were not given a file, allocate a readahead
  1133. * context
  1134. */
  1135. if (!file) {
  1136. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  1137. if (!ra)
  1138. return -ENOMEM;
  1139. file_ra_state_init(ra, inode->i_mapping);
  1140. } else {
  1141. ra = &file->f_ra;
  1142. }
  1143. pages = kmalloc_array(max_cluster, sizeof(struct page *),
  1144. GFP_NOFS);
  1145. if (!pages) {
  1146. ret = -ENOMEM;
  1147. goto out_ra;
  1148. }
  1149. /* find the last page to defrag */
  1150. if (range->start + range->len > range->start) {
  1151. last_index = min_t(u64, isize - 1,
  1152. range->start + range->len - 1) >> PAGE_SHIFT;
  1153. } else {
  1154. last_index = (isize - 1) >> PAGE_SHIFT;
  1155. }
  1156. if (newer_than) {
  1157. ret = find_new_extents(root, inode, newer_than,
  1158. &newer_off, SZ_64K);
  1159. if (!ret) {
  1160. range->start = newer_off;
  1161. /*
  1162. * we always align our defrag to help keep
  1163. * the extents in the file evenly spaced
  1164. */
  1165. i = (newer_off & new_align) >> PAGE_SHIFT;
  1166. } else
  1167. goto out_ra;
  1168. } else {
  1169. i = range->start >> PAGE_SHIFT;
  1170. }
  1171. if (!max_to_defrag)
  1172. max_to_defrag = last_index - i + 1;
  1173. /*
  1174. * make writeback starts from i, so the defrag range can be
  1175. * written sequentially.
  1176. */
  1177. if (i < inode->i_mapping->writeback_index)
  1178. inode->i_mapping->writeback_index = i;
  1179. while (i <= last_index && defrag_count < max_to_defrag &&
  1180. (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
  1181. /*
  1182. * make sure we stop running if someone unmounts
  1183. * the FS
  1184. */
  1185. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1186. break;
  1187. if (btrfs_defrag_cancelled(root->fs_info)) {
  1188. btrfs_debug(root->fs_info, "defrag_file cancelled");
  1189. ret = -EAGAIN;
  1190. break;
  1191. }
  1192. if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
  1193. extent_thresh, &last_len, &skip,
  1194. &defrag_end, range->flags &
  1195. BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1196. unsigned long next;
  1197. /*
  1198. * the should_defrag function tells us how much to skip
  1199. * bump our counter by the suggested amount
  1200. */
  1201. next = DIV_ROUND_UP(skip, PAGE_SIZE);
  1202. i = max(i + 1, next);
  1203. continue;
  1204. }
  1205. if (!newer_than) {
  1206. cluster = (PAGE_ALIGN(defrag_end) >>
  1207. PAGE_SHIFT) - i;
  1208. cluster = min(cluster, max_cluster);
  1209. } else {
  1210. cluster = max_cluster;
  1211. }
  1212. if (i + cluster > ra_index) {
  1213. ra_index = max(i, ra_index);
  1214. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  1215. cluster);
  1216. ra_index += cluster;
  1217. }
  1218. inode_lock(inode);
  1219. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  1220. BTRFS_I(inode)->force_compress = compress_type;
  1221. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1222. if (ret < 0) {
  1223. inode_unlock(inode);
  1224. goto out_ra;
  1225. }
  1226. defrag_count += ret;
  1227. balance_dirty_pages_ratelimited(inode->i_mapping);
  1228. inode_unlock(inode);
  1229. if (newer_than) {
  1230. if (newer_off == (u64)-1)
  1231. break;
  1232. if (ret > 0)
  1233. i += ret;
  1234. newer_off = max(newer_off + 1,
  1235. (u64)i << PAGE_SHIFT);
  1236. ret = find_new_extents(root, inode, newer_than,
  1237. &newer_off, SZ_64K);
  1238. if (!ret) {
  1239. range->start = newer_off;
  1240. i = (newer_off & new_align) >> PAGE_SHIFT;
  1241. } else {
  1242. break;
  1243. }
  1244. } else {
  1245. if (ret > 0) {
  1246. i += ret;
  1247. last_len += ret << PAGE_SHIFT;
  1248. } else {
  1249. i++;
  1250. last_len = 0;
  1251. }
  1252. }
  1253. }
  1254. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
  1255. filemap_flush(inode->i_mapping);
  1256. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1257. &BTRFS_I(inode)->runtime_flags))
  1258. filemap_flush(inode->i_mapping);
  1259. }
  1260. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1261. /* the filemap_flush will queue IO into the worker threads, but
  1262. * we have to make sure the IO is actually started and that
  1263. * ordered extents get created before we return
  1264. */
  1265. atomic_inc(&root->fs_info->async_submit_draining);
  1266. while (atomic_read(&root->fs_info->nr_async_submits) ||
  1267. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1268. wait_event(root->fs_info->async_submit_wait,
  1269. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  1270. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  1271. }
  1272. atomic_dec(&root->fs_info->async_submit_draining);
  1273. }
  1274. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1275. btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
  1276. }
  1277. ret = defrag_count;
  1278. out_ra:
  1279. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1280. inode_lock(inode);
  1281. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1282. inode_unlock(inode);
  1283. }
  1284. if (!file)
  1285. kfree(ra);
  1286. kfree(pages);
  1287. return ret;
  1288. }
  1289. static noinline int btrfs_ioctl_resize(struct file *file,
  1290. void __user *arg)
  1291. {
  1292. u64 new_size;
  1293. u64 old_size;
  1294. u64 devid = 1;
  1295. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  1296. struct btrfs_ioctl_vol_args *vol_args;
  1297. struct btrfs_trans_handle *trans;
  1298. struct btrfs_device *device = NULL;
  1299. char *sizestr;
  1300. char *retptr;
  1301. char *devstr = NULL;
  1302. int ret = 0;
  1303. int mod = 0;
  1304. if (!capable(CAP_SYS_ADMIN))
  1305. return -EPERM;
  1306. ret = mnt_want_write_file(file);
  1307. if (ret)
  1308. return ret;
  1309. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1310. 1)) {
  1311. mnt_drop_write_file(file);
  1312. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  1313. }
  1314. mutex_lock(&root->fs_info->volume_mutex);
  1315. vol_args = memdup_user(arg, sizeof(*vol_args));
  1316. if (IS_ERR(vol_args)) {
  1317. ret = PTR_ERR(vol_args);
  1318. goto out;
  1319. }
  1320. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1321. sizestr = vol_args->name;
  1322. devstr = strchr(sizestr, ':');
  1323. if (devstr) {
  1324. sizestr = devstr + 1;
  1325. *devstr = '\0';
  1326. devstr = vol_args->name;
  1327. ret = kstrtoull(devstr, 10, &devid);
  1328. if (ret)
  1329. goto out_free;
  1330. if (!devid) {
  1331. ret = -EINVAL;
  1332. goto out_free;
  1333. }
  1334. btrfs_info(root->fs_info, "resizing devid %llu", devid);
  1335. }
  1336. device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  1337. if (!device) {
  1338. btrfs_info(root->fs_info, "resizer unable to find device %llu",
  1339. devid);
  1340. ret = -ENODEV;
  1341. goto out_free;
  1342. }
  1343. if (!device->writeable) {
  1344. btrfs_info(root->fs_info,
  1345. "resizer unable to apply on readonly device %llu",
  1346. devid);
  1347. ret = -EPERM;
  1348. goto out_free;
  1349. }
  1350. if (!strcmp(sizestr, "max"))
  1351. new_size = device->bdev->bd_inode->i_size;
  1352. else {
  1353. if (sizestr[0] == '-') {
  1354. mod = -1;
  1355. sizestr++;
  1356. } else if (sizestr[0] == '+') {
  1357. mod = 1;
  1358. sizestr++;
  1359. }
  1360. new_size = memparse(sizestr, &retptr);
  1361. if (*retptr != '\0' || new_size == 0) {
  1362. ret = -EINVAL;
  1363. goto out_free;
  1364. }
  1365. }
  1366. if (device->is_tgtdev_for_dev_replace) {
  1367. ret = -EPERM;
  1368. goto out_free;
  1369. }
  1370. old_size = btrfs_device_get_total_bytes(device);
  1371. if (mod < 0) {
  1372. if (new_size > old_size) {
  1373. ret = -EINVAL;
  1374. goto out_free;
  1375. }
  1376. new_size = old_size - new_size;
  1377. } else if (mod > 0) {
  1378. if (new_size > ULLONG_MAX - old_size) {
  1379. ret = -ERANGE;
  1380. goto out_free;
  1381. }
  1382. new_size = old_size + new_size;
  1383. }
  1384. if (new_size < SZ_256M) {
  1385. ret = -EINVAL;
  1386. goto out_free;
  1387. }
  1388. if (new_size > device->bdev->bd_inode->i_size) {
  1389. ret = -EFBIG;
  1390. goto out_free;
  1391. }
  1392. new_size = div_u64(new_size, root->sectorsize);
  1393. new_size *= root->sectorsize;
  1394. btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
  1395. rcu_str_deref(device->name), new_size);
  1396. if (new_size > old_size) {
  1397. trans = btrfs_start_transaction(root, 0);
  1398. if (IS_ERR(trans)) {
  1399. ret = PTR_ERR(trans);
  1400. goto out_free;
  1401. }
  1402. ret = btrfs_grow_device(trans, device, new_size);
  1403. btrfs_commit_transaction(trans, root);
  1404. } else if (new_size < old_size) {
  1405. ret = btrfs_shrink_device(device, new_size);
  1406. } /* equal, nothing need to do */
  1407. out_free:
  1408. kfree(vol_args);
  1409. out:
  1410. mutex_unlock(&root->fs_info->volume_mutex);
  1411. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1412. mnt_drop_write_file(file);
  1413. return ret;
  1414. }
  1415. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1416. char *name, unsigned long fd, int subvol,
  1417. u64 *transid, bool readonly,
  1418. struct btrfs_qgroup_inherit *inherit)
  1419. {
  1420. int namelen;
  1421. int ret = 0;
  1422. if (!S_ISDIR(file_inode(file)->i_mode))
  1423. return -ENOTDIR;
  1424. ret = mnt_want_write_file(file);
  1425. if (ret)
  1426. goto out;
  1427. namelen = strlen(name);
  1428. if (strchr(name, '/')) {
  1429. ret = -EINVAL;
  1430. goto out_drop_write;
  1431. }
  1432. if (name[0] == '.' &&
  1433. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1434. ret = -EEXIST;
  1435. goto out_drop_write;
  1436. }
  1437. if (subvol) {
  1438. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1439. NULL, transid, readonly, inherit);
  1440. } else {
  1441. struct fd src = fdget(fd);
  1442. struct inode *src_inode;
  1443. if (!src.file) {
  1444. ret = -EINVAL;
  1445. goto out_drop_write;
  1446. }
  1447. src_inode = file_inode(src.file);
  1448. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1449. btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
  1450. "Snapshot src from another FS");
  1451. ret = -EXDEV;
  1452. } else if (!inode_owner_or_capable(src_inode)) {
  1453. /*
  1454. * Subvolume creation is not restricted, but snapshots
  1455. * are limited to own subvolumes only
  1456. */
  1457. ret = -EPERM;
  1458. } else {
  1459. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1460. BTRFS_I(src_inode)->root,
  1461. transid, readonly, inherit);
  1462. }
  1463. fdput(src);
  1464. }
  1465. out_drop_write:
  1466. mnt_drop_write_file(file);
  1467. out:
  1468. return ret;
  1469. }
  1470. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1471. void __user *arg, int subvol)
  1472. {
  1473. struct btrfs_ioctl_vol_args *vol_args;
  1474. int ret;
  1475. if (!S_ISDIR(file_inode(file)->i_mode))
  1476. return -ENOTDIR;
  1477. vol_args = memdup_user(arg, sizeof(*vol_args));
  1478. if (IS_ERR(vol_args))
  1479. return PTR_ERR(vol_args);
  1480. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1481. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1482. vol_args->fd, subvol,
  1483. NULL, false, NULL);
  1484. kfree(vol_args);
  1485. return ret;
  1486. }
  1487. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1488. void __user *arg, int subvol)
  1489. {
  1490. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1491. int ret;
  1492. u64 transid = 0;
  1493. u64 *ptr = NULL;
  1494. bool readonly = false;
  1495. struct btrfs_qgroup_inherit *inherit = NULL;
  1496. if (!S_ISDIR(file_inode(file)->i_mode))
  1497. return -ENOTDIR;
  1498. vol_args = memdup_user(arg, sizeof(*vol_args));
  1499. if (IS_ERR(vol_args))
  1500. return PTR_ERR(vol_args);
  1501. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1502. if (vol_args->flags &
  1503. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1504. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1505. ret = -EOPNOTSUPP;
  1506. goto free_args;
  1507. }
  1508. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1509. ptr = &transid;
  1510. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1511. readonly = true;
  1512. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1513. if (vol_args->size > PAGE_SIZE) {
  1514. ret = -EINVAL;
  1515. goto free_args;
  1516. }
  1517. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1518. if (IS_ERR(inherit)) {
  1519. ret = PTR_ERR(inherit);
  1520. goto free_args;
  1521. }
  1522. }
  1523. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1524. vol_args->fd, subvol, ptr,
  1525. readonly, inherit);
  1526. if (ret)
  1527. goto free_inherit;
  1528. if (ptr && copy_to_user(arg +
  1529. offsetof(struct btrfs_ioctl_vol_args_v2,
  1530. transid),
  1531. ptr, sizeof(*ptr)))
  1532. ret = -EFAULT;
  1533. free_inherit:
  1534. kfree(inherit);
  1535. free_args:
  1536. kfree(vol_args);
  1537. return ret;
  1538. }
  1539. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1540. void __user *arg)
  1541. {
  1542. struct inode *inode = file_inode(file);
  1543. struct btrfs_root *root = BTRFS_I(inode)->root;
  1544. int ret = 0;
  1545. u64 flags = 0;
  1546. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1547. return -EINVAL;
  1548. down_read(&root->fs_info->subvol_sem);
  1549. if (btrfs_root_readonly(root))
  1550. flags |= BTRFS_SUBVOL_RDONLY;
  1551. up_read(&root->fs_info->subvol_sem);
  1552. if (copy_to_user(arg, &flags, sizeof(flags)))
  1553. ret = -EFAULT;
  1554. return ret;
  1555. }
  1556. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1557. void __user *arg)
  1558. {
  1559. struct inode *inode = file_inode(file);
  1560. struct btrfs_root *root = BTRFS_I(inode)->root;
  1561. struct btrfs_trans_handle *trans;
  1562. u64 root_flags;
  1563. u64 flags;
  1564. int ret = 0;
  1565. if (!inode_owner_or_capable(inode))
  1566. return -EPERM;
  1567. ret = mnt_want_write_file(file);
  1568. if (ret)
  1569. goto out;
  1570. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1571. ret = -EINVAL;
  1572. goto out_drop_write;
  1573. }
  1574. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1575. ret = -EFAULT;
  1576. goto out_drop_write;
  1577. }
  1578. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1579. ret = -EINVAL;
  1580. goto out_drop_write;
  1581. }
  1582. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1583. ret = -EOPNOTSUPP;
  1584. goto out_drop_write;
  1585. }
  1586. down_write(&root->fs_info->subvol_sem);
  1587. /* nothing to do */
  1588. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1589. goto out_drop_sem;
  1590. root_flags = btrfs_root_flags(&root->root_item);
  1591. if (flags & BTRFS_SUBVOL_RDONLY) {
  1592. btrfs_set_root_flags(&root->root_item,
  1593. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1594. } else {
  1595. /*
  1596. * Block RO -> RW transition if this subvolume is involved in
  1597. * send
  1598. */
  1599. spin_lock(&root->root_item_lock);
  1600. if (root->send_in_progress == 0) {
  1601. btrfs_set_root_flags(&root->root_item,
  1602. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1603. spin_unlock(&root->root_item_lock);
  1604. } else {
  1605. spin_unlock(&root->root_item_lock);
  1606. btrfs_warn(root->fs_info,
  1607. "Attempt to set subvolume %llu read-write during send",
  1608. root->root_key.objectid);
  1609. ret = -EPERM;
  1610. goto out_drop_sem;
  1611. }
  1612. }
  1613. trans = btrfs_start_transaction(root, 1);
  1614. if (IS_ERR(trans)) {
  1615. ret = PTR_ERR(trans);
  1616. goto out_reset;
  1617. }
  1618. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1619. &root->root_key, &root->root_item);
  1620. btrfs_commit_transaction(trans, root);
  1621. out_reset:
  1622. if (ret)
  1623. btrfs_set_root_flags(&root->root_item, root_flags);
  1624. out_drop_sem:
  1625. up_write(&root->fs_info->subvol_sem);
  1626. out_drop_write:
  1627. mnt_drop_write_file(file);
  1628. out:
  1629. return ret;
  1630. }
  1631. /*
  1632. * helper to check if the subvolume references other subvolumes
  1633. */
  1634. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1635. {
  1636. struct btrfs_path *path;
  1637. struct btrfs_dir_item *di;
  1638. struct btrfs_key key;
  1639. u64 dir_id;
  1640. int ret;
  1641. path = btrfs_alloc_path();
  1642. if (!path)
  1643. return -ENOMEM;
  1644. /* Make sure this root isn't set as the default subvol */
  1645. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  1646. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
  1647. dir_id, "default", 7, 0);
  1648. if (di && !IS_ERR(di)) {
  1649. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1650. if (key.objectid == root->root_key.objectid) {
  1651. ret = -EPERM;
  1652. btrfs_err(root->fs_info,
  1653. "deleting default subvolume %llu is not allowed",
  1654. key.objectid);
  1655. goto out;
  1656. }
  1657. btrfs_release_path(path);
  1658. }
  1659. key.objectid = root->root_key.objectid;
  1660. key.type = BTRFS_ROOT_REF_KEY;
  1661. key.offset = (u64)-1;
  1662. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1663. &key, path, 0, 0);
  1664. if (ret < 0)
  1665. goto out;
  1666. BUG_ON(ret == 0);
  1667. ret = 0;
  1668. if (path->slots[0] > 0) {
  1669. path->slots[0]--;
  1670. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1671. if (key.objectid == root->root_key.objectid &&
  1672. key.type == BTRFS_ROOT_REF_KEY)
  1673. ret = -ENOTEMPTY;
  1674. }
  1675. out:
  1676. btrfs_free_path(path);
  1677. return ret;
  1678. }
  1679. static noinline int key_in_sk(struct btrfs_key *key,
  1680. struct btrfs_ioctl_search_key *sk)
  1681. {
  1682. struct btrfs_key test;
  1683. int ret;
  1684. test.objectid = sk->min_objectid;
  1685. test.type = sk->min_type;
  1686. test.offset = sk->min_offset;
  1687. ret = btrfs_comp_cpu_keys(key, &test);
  1688. if (ret < 0)
  1689. return 0;
  1690. test.objectid = sk->max_objectid;
  1691. test.type = sk->max_type;
  1692. test.offset = sk->max_offset;
  1693. ret = btrfs_comp_cpu_keys(key, &test);
  1694. if (ret > 0)
  1695. return 0;
  1696. return 1;
  1697. }
  1698. static noinline int copy_to_sk(struct btrfs_path *path,
  1699. struct btrfs_key *key,
  1700. struct btrfs_ioctl_search_key *sk,
  1701. size_t *buf_size,
  1702. char __user *ubuf,
  1703. unsigned long *sk_offset,
  1704. int *num_found)
  1705. {
  1706. u64 found_transid;
  1707. struct extent_buffer *leaf;
  1708. struct btrfs_ioctl_search_header sh;
  1709. struct btrfs_key test;
  1710. unsigned long item_off;
  1711. unsigned long item_len;
  1712. int nritems;
  1713. int i;
  1714. int slot;
  1715. int ret = 0;
  1716. leaf = path->nodes[0];
  1717. slot = path->slots[0];
  1718. nritems = btrfs_header_nritems(leaf);
  1719. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1720. i = nritems;
  1721. goto advance_key;
  1722. }
  1723. found_transid = btrfs_header_generation(leaf);
  1724. for (i = slot; i < nritems; i++) {
  1725. item_off = btrfs_item_ptr_offset(leaf, i);
  1726. item_len = btrfs_item_size_nr(leaf, i);
  1727. btrfs_item_key_to_cpu(leaf, key, i);
  1728. if (!key_in_sk(key, sk))
  1729. continue;
  1730. if (sizeof(sh) + item_len > *buf_size) {
  1731. if (*num_found) {
  1732. ret = 1;
  1733. goto out;
  1734. }
  1735. /*
  1736. * return one empty item back for v1, which does not
  1737. * handle -EOVERFLOW
  1738. */
  1739. *buf_size = sizeof(sh) + item_len;
  1740. item_len = 0;
  1741. ret = -EOVERFLOW;
  1742. }
  1743. if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
  1744. ret = 1;
  1745. goto out;
  1746. }
  1747. sh.objectid = key->objectid;
  1748. sh.offset = key->offset;
  1749. sh.type = key->type;
  1750. sh.len = item_len;
  1751. sh.transid = found_transid;
  1752. /* copy search result header */
  1753. if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
  1754. ret = -EFAULT;
  1755. goto out;
  1756. }
  1757. *sk_offset += sizeof(sh);
  1758. if (item_len) {
  1759. char __user *up = ubuf + *sk_offset;
  1760. /* copy the item */
  1761. if (read_extent_buffer_to_user(leaf, up,
  1762. item_off, item_len)) {
  1763. ret = -EFAULT;
  1764. goto out;
  1765. }
  1766. *sk_offset += item_len;
  1767. }
  1768. (*num_found)++;
  1769. if (ret) /* -EOVERFLOW from above */
  1770. goto out;
  1771. if (*num_found >= sk->nr_items) {
  1772. ret = 1;
  1773. goto out;
  1774. }
  1775. }
  1776. advance_key:
  1777. ret = 0;
  1778. test.objectid = sk->max_objectid;
  1779. test.type = sk->max_type;
  1780. test.offset = sk->max_offset;
  1781. if (btrfs_comp_cpu_keys(key, &test) >= 0)
  1782. ret = 1;
  1783. else if (key->offset < (u64)-1)
  1784. key->offset++;
  1785. else if (key->type < (u8)-1) {
  1786. key->offset = 0;
  1787. key->type++;
  1788. } else if (key->objectid < (u64)-1) {
  1789. key->offset = 0;
  1790. key->type = 0;
  1791. key->objectid++;
  1792. } else
  1793. ret = 1;
  1794. out:
  1795. /*
  1796. * 0: all items from this leaf copied, continue with next
  1797. * 1: * more items can be copied, but unused buffer is too small
  1798. * * all items were found
  1799. * Either way, it will stops the loop which iterates to the next
  1800. * leaf
  1801. * -EOVERFLOW: item was to large for buffer
  1802. * -EFAULT: could not copy extent buffer back to userspace
  1803. */
  1804. return ret;
  1805. }
  1806. static noinline int search_ioctl(struct inode *inode,
  1807. struct btrfs_ioctl_search_key *sk,
  1808. size_t *buf_size,
  1809. char __user *ubuf)
  1810. {
  1811. struct btrfs_root *root;
  1812. struct btrfs_key key;
  1813. struct btrfs_path *path;
  1814. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1815. int ret;
  1816. int num_found = 0;
  1817. unsigned long sk_offset = 0;
  1818. if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
  1819. *buf_size = sizeof(struct btrfs_ioctl_search_header);
  1820. return -EOVERFLOW;
  1821. }
  1822. path = btrfs_alloc_path();
  1823. if (!path)
  1824. return -ENOMEM;
  1825. if (sk->tree_id == 0) {
  1826. /* search the root of the inode that was passed */
  1827. root = BTRFS_I(inode)->root;
  1828. } else {
  1829. key.objectid = sk->tree_id;
  1830. key.type = BTRFS_ROOT_ITEM_KEY;
  1831. key.offset = (u64)-1;
  1832. root = btrfs_read_fs_root_no_name(info, &key);
  1833. if (IS_ERR(root)) {
  1834. btrfs_free_path(path);
  1835. return -ENOENT;
  1836. }
  1837. }
  1838. key.objectid = sk->min_objectid;
  1839. key.type = sk->min_type;
  1840. key.offset = sk->min_offset;
  1841. while (1) {
  1842. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1843. if (ret != 0) {
  1844. if (ret > 0)
  1845. ret = 0;
  1846. goto err;
  1847. }
  1848. ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
  1849. &sk_offset, &num_found);
  1850. btrfs_release_path(path);
  1851. if (ret)
  1852. break;
  1853. }
  1854. if (ret > 0)
  1855. ret = 0;
  1856. err:
  1857. sk->nr_items = num_found;
  1858. btrfs_free_path(path);
  1859. return ret;
  1860. }
  1861. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1862. void __user *argp)
  1863. {
  1864. struct btrfs_ioctl_search_args __user *uargs;
  1865. struct btrfs_ioctl_search_key sk;
  1866. struct inode *inode;
  1867. int ret;
  1868. size_t buf_size;
  1869. if (!capable(CAP_SYS_ADMIN))
  1870. return -EPERM;
  1871. uargs = (struct btrfs_ioctl_search_args __user *)argp;
  1872. if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
  1873. return -EFAULT;
  1874. buf_size = sizeof(uargs->buf);
  1875. inode = file_inode(file);
  1876. ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
  1877. /*
  1878. * In the origin implementation an overflow is handled by returning a
  1879. * search header with a len of zero, so reset ret.
  1880. */
  1881. if (ret == -EOVERFLOW)
  1882. ret = 0;
  1883. if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
  1884. ret = -EFAULT;
  1885. return ret;
  1886. }
  1887. static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
  1888. void __user *argp)
  1889. {
  1890. struct btrfs_ioctl_search_args_v2 __user *uarg;
  1891. struct btrfs_ioctl_search_args_v2 args;
  1892. struct inode *inode;
  1893. int ret;
  1894. size_t buf_size;
  1895. const size_t buf_limit = SZ_16M;
  1896. if (!capable(CAP_SYS_ADMIN))
  1897. return -EPERM;
  1898. /* copy search header and buffer size */
  1899. uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
  1900. if (copy_from_user(&args, uarg, sizeof(args)))
  1901. return -EFAULT;
  1902. buf_size = args.buf_size;
  1903. if (buf_size < sizeof(struct btrfs_ioctl_search_header))
  1904. return -EOVERFLOW;
  1905. /* limit result size to 16MB */
  1906. if (buf_size > buf_limit)
  1907. buf_size = buf_limit;
  1908. inode = file_inode(file);
  1909. ret = search_ioctl(inode, &args.key, &buf_size,
  1910. (char *)(&uarg->buf[0]));
  1911. if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
  1912. ret = -EFAULT;
  1913. else if (ret == -EOVERFLOW &&
  1914. copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
  1915. ret = -EFAULT;
  1916. return ret;
  1917. }
  1918. /*
  1919. * Search INODE_REFs to identify path name of 'dirid' directory
  1920. * in a 'tree_id' tree. and sets path name to 'name'.
  1921. */
  1922. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1923. u64 tree_id, u64 dirid, char *name)
  1924. {
  1925. struct btrfs_root *root;
  1926. struct btrfs_key key;
  1927. char *ptr;
  1928. int ret = -1;
  1929. int slot;
  1930. int len;
  1931. int total_len = 0;
  1932. struct btrfs_inode_ref *iref;
  1933. struct extent_buffer *l;
  1934. struct btrfs_path *path;
  1935. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1936. name[0]='\0';
  1937. return 0;
  1938. }
  1939. path = btrfs_alloc_path();
  1940. if (!path)
  1941. return -ENOMEM;
  1942. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1943. key.objectid = tree_id;
  1944. key.type = BTRFS_ROOT_ITEM_KEY;
  1945. key.offset = (u64)-1;
  1946. root = btrfs_read_fs_root_no_name(info, &key);
  1947. if (IS_ERR(root)) {
  1948. btrfs_err(info, "could not find root %llu", tree_id);
  1949. ret = -ENOENT;
  1950. goto out;
  1951. }
  1952. key.objectid = dirid;
  1953. key.type = BTRFS_INODE_REF_KEY;
  1954. key.offset = (u64)-1;
  1955. while (1) {
  1956. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1957. if (ret < 0)
  1958. goto out;
  1959. else if (ret > 0) {
  1960. ret = btrfs_previous_item(root, path, dirid,
  1961. BTRFS_INODE_REF_KEY);
  1962. if (ret < 0)
  1963. goto out;
  1964. else if (ret > 0) {
  1965. ret = -ENOENT;
  1966. goto out;
  1967. }
  1968. }
  1969. l = path->nodes[0];
  1970. slot = path->slots[0];
  1971. btrfs_item_key_to_cpu(l, &key, slot);
  1972. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1973. len = btrfs_inode_ref_name_len(l, iref);
  1974. ptr -= len + 1;
  1975. total_len += len + 1;
  1976. if (ptr < name) {
  1977. ret = -ENAMETOOLONG;
  1978. goto out;
  1979. }
  1980. *(ptr + len) = '/';
  1981. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  1982. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1983. break;
  1984. btrfs_release_path(path);
  1985. key.objectid = key.offset;
  1986. key.offset = (u64)-1;
  1987. dirid = key.objectid;
  1988. }
  1989. memmove(name, ptr, total_len);
  1990. name[total_len] = '\0';
  1991. ret = 0;
  1992. out:
  1993. btrfs_free_path(path);
  1994. return ret;
  1995. }
  1996. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1997. void __user *argp)
  1998. {
  1999. struct btrfs_ioctl_ino_lookup_args *args;
  2000. struct inode *inode;
  2001. int ret = 0;
  2002. args = memdup_user(argp, sizeof(*args));
  2003. if (IS_ERR(args))
  2004. return PTR_ERR(args);
  2005. inode = file_inode(file);
  2006. /*
  2007. * Unprivileged query to obtain the containing subvolume root id. The
  2008. * path is reset so it's consistent with btrfs_search_path_in_tree.
  2009. */
  2010. if (args->treeid == 0)
  2011. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  2012. if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
  2013. args->name[0] = 0;
  2014. goto out;
  2015. }
  2016. if (!capable(CAP_SYS_ADMIN)) {
  2017. ret = -EPERM;
  2018. goto out;
  2019. }
  2020. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  2021. args->treeid, args->objectid,
  2022. args->name);
  2023. out:
  2024. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  2025. ret = -EFAULT;
  2026. kfree(args);
  2027. return ret;
  2028. }
  2029. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  2030. void __user *arg)
  2031. {
  2032. struct dentry *parent = file->f_path.dentry;
  2033. struct dentry *dentry;
  2034. struct inode *dir = d_inode(parent);
  2035. struct inode *inode;
  2036. struct btrfs_root *root = BTRFS_I(dir)->root;
  2037. struct btrfs_root *dest = NULL;
  2038. struct btrfs_ioctl_vol_args *vol_args;
  2039. struct btrfs_trans_handle *trans;
  2040. struct btrfs_block_rsv block_rsv;
  2041. u64 root_flags;
  2042. u64 qgroup_reserved;
  2043. int namelen;
  2044. int ret;
  2045. int err = 0;
  2046. if (!S_ISDIR(dir->i_mode))
  2047. return -ENOTDIR;
  2048. vol_args = memdup_user(arg, sizeof(*vol_args));
  2049. if (IS_ERR(vol_args))
  2050. return PTR_ERR(vol_args);
  2051. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2052. namelen = strlen(vol_args->name);
  2053. if (strchr(vol_args->name, '/') ||
  2054. strncmp(vol_args->name, "..", namelen) == 0) {
  2055. err = -EINVAL;
  2056. goto out;
  2057. }
  2058. err = mnt_want_write_file(file);
  2059. if (err)
  2060. goto out;
  2061. err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
  2062. if (err == -EINTR)
  2063. goto out_drop_write;
  2064. dentry = lookup_one_len(vol_args->name, parent, namelen);
  2065. if (IS_ERR(dentry)) {
  2066. err = PTR_ERR(dentry);
  2067. goto out_unlock_dir;
  2068. }
  2069. if (d_really_is_negative(dentry)) {
  2070. err = -ENOENT;
  2071. goto out_dput;
  2072. }
  2073. inode = d_inode(dentry);
  2074. dest = BTRFS_I(inode)->root;
  2075. if (!capable(CAP_SYS_ADMIN)) {
  2076. /*
  2077. * Regular user. Only allow this with a special mount
  2078. * option, when the user has write+exec access to the
  2079. * subvol root, and when rmdir(2) would have been
  2080. * allowed.
  2081. *
  2082. * Note that this is _not_ check that the subvol is
  2083. * empty or doesn't contain data that we wouldn't
  2084. * otherwise be able to delete.
  2085. *
  2086. * Users who want to delete empty subvols should try
  2087. * rmdir(2).
  2088. */
  2089. err = -EPERM;
  2090. if (!btrfs_test_opt(root->fs_info, USER_SUBVOL_RM_ALLOWED))
  2091. goto out_dput;
  2092. /*
  2093. * Do not allow deletion if the parent dir is the same
  2094. * as the dir to be deleted. That means the ioctl
  2095. * must be called on the dentry referencing the root
  2096. * of the subvol, not a random directory contained
  2097. * within it.
  2098. */
  2099. err = -EINVAL;
  2100. if (root == dest)
  2101. goto out_dput;
  2102. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  2103. if (err)
  2104. goto out_dput;
  2105. }
  2106. /* check if subvolume may be deleted by a user */
  2107. err = btrfs_may_delete(dir, dentry, 1);
  2108. if (err)
  2109. goto out_dput;
  2110. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  2111. err = -EINVAL;
  2112. goto out_dput;
  2113. }
  2114. inode_lock(inode);
  2115. /*
  2116. * Don't allow to delete a subvolume with send in progress. This is
  2117. * inside the i_mutex so the error handling that has to drop the bit
  2118. * again is not run concurrently.
  2119. */
  2120. spin_lock(&dest->root_item_lock);
  2121. root_flags = btrfs_root_flags(&dest->root_item);
  2122. if (dest->send_in_progress == 0) {
  2123. btrfs_set_root_flags(&dest->root_item,
  2124. root_flags | BTRFS_ROOT_SUBVOL_DEAD);
  2125. spin_unlock(&dest->root_item_lock);
  2126. } else {
  2127. spin_unlock(&dest->root_item_lock);
  2128. btrfs_warn(root->fs_info,
  2129. "Attempt to delete subvolume %llu during send",
  2130. dest->root_key.objectid);
  2131. err = -EPERM;
  2132. goto out_unlock_inode;
  2133. }
  2134. down_write(&root->fs_info->subvol_sem);
  2135. err = may_destroy_subvol(dest);
  2136. if (err)
  2137. goto out_up_write;
  2138. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  2139. /*
  2140. * One for dir inode, two for dir entries, two for root
  2141. * ref/backref.
  2142. */
  2143. err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  2144. 5, &qgroup_reserved, true);
  2145. if (err)
  2146. goto out_up_write;
  2147. trans = btrfs_start_transaction(root, 0);
  2148. if (IS_ERR(trans)) {
  2149. err = PTR_ERR(trans);
  2150. goto out_release;
  2151. }
  2152. trans->block_rsv = &block_rsv;
  2153. trans->bytes_reserved = block_rsv.size;
  2154. btrfs_record_snapshot_destroy(trans, dir);
  2155. ret = btrfs_unlink_subvol(trans, root, dir,
  2156. dest->root_key.objectid,
  2157. dentry->d_name.name,
  2158. dentry->d_name.len);
  2159. if (ret) {
  2160. err = ret;
  2161. btrfs_abort_transaction(trans, ret);
  2162. goto out_end_trans;
  2163. }
  2164. btrfs_record_root_in_trans(trans, dest);
  2165. memset(&dest->root_item.drop_progress, 0,
  2166. sizeof(dest->root_item.drop_progress));
  2167. dest->root_item.drop_level = 0;
  2168. btrfs_set_root_refs(&dest->root_item, 0);
  2169. if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
  2170. ret = btrfs_insert_orphan_item(trans,
  2171. root->fs_info->tree_root,
  2172. dest->root_key.objectid);
  2173. if (ret) {
  2174. btrfs_abort_transaction(trans, ret);
  2175. err = ret;
  2176. goto out_end_trans;
  2177. }
  2178. }
  2179. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  2180. dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  2181. dest->root_key.objectid);
  2182. if (ret && ret != -ENOENT) {
  2183. btrfs_abort_transaction(trans, ret);
  2184. err = ret;
  2185. goto out_end_trans;
  2186. }
  2187. if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
  2188. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  2189. dest->root_item.received_uuid,
  2190. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  2191. dest->root_key.objectid);
  2192. if (ret && ret != -ENOENT) {
  2193. btrfs_abort_transaction(trans, ret);
  2194. err = ret;
  2195. goto out_end_trans;
  2196. }
  2197. }
  2198. out_end_trans:
  2199. trans->block_rsv = NULL;
  2200. trans->bytes_reserved = 0;
  2201. ret = btrfs_end_transaction(trans, root);
  2202. if (ret && !err)
  2203. err = ret;
  2204. inode->i_flags |= S_DEAD;
  2205. out_release:
  2206. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  2207. out_up_write:
  2208. up_write(&root->fs_info->subvol_sem);
  2209. if (err) {
  2210. spin_lock(&dest->root_item_lock);
  2211. root_flags = btrfs_root_flags(&dest->root_item);
  2212. btrfs_set_root_flags(&dest->root_item,
  2213. root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
  2214. spin_unlock(&dest->root_item_lock);
  2215. }
  2216. out_unlock_inode:
  2217. inode_unlock(inode);
  2218. if (!err) {
  2219. d_invalidate(dentry);
  2220. btrfs_invalidate_inodes(dest);
  2221. d_delete(dentry);
  2222. ASSERT(dest->send_in_progress == 0);
  2223. /* the last ref */
  2224. if (dest->ino_cache_inode) {
  2225. iput(dest->ino_cache_inode);
  2226. dest->ino_cache_inode = NULL;
  2227. }
  2228. }
  2229. out_dput:
  2230. dput(dentry);
  2231. out_unlock_dir:
  2232. inode_unlock(dir);
  2233. out_drop_write:
  2234. mnt_drop_write_file(file);
  2235. out:
  2236. kfree(vol_args);
  2237. return err;
  2238. }
  2239. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2240. {
  2241. struct inode *inode = file_inode(file);
  2242. struct btrfs_root *root = BTRFS_I(inode)->root;
  2243. struct btrfs_ioctl_defrag_range_args *range;
  2244. int ret;
  2245. ret = mnt_want_write_file(file);
  2246. if (ret)
  2247. return ret;
  2248. if (btrfs_root_readonly(root)) {
  2249. ret = -EROFS;
  2250. goto out;
  2251. }
  2252. switch (inode->i_mode & S_IFMT) {
  2253. case S_IFDIR:
  2254. if (!capable(CAP_SYS_ADMIN)) {
  2255. ret = -EPERM;
  2256. goto out;
  2257. }
  2258. ret = btrfs_defrag_root(root);
  2259. if (ret)
  2260. goto out;
  2261. ret = btrfs_defrag_root(root->fs_info->extent_root);
  2262. break;
  2263. case S_IFREG:
  2264. if (!(file->f_mode & FMODE_WRITE)) {
  2265. ret = -EINVAL;
  2266. goto out;
  2267. }
  2268. range = kzalloc(sizeof(*range), GFP_KERNEL);
  2269. if (!range) {
  2270. ret = -ENOMEM;
  2271. goto out;
  2272. }
  2273. if (argp) {
  2274. if (copy_from_user(range, argp,
  2275. sizeof(*range))) {
  2276. ret = -EFAULT;
  2277. kfree(range);
  2278. goto out;
  2279. }
  2280. /* compression requires us to start the IO */
  2281. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2282. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2283. range->extent_thresh = (u32)-1;
  2284. }
  2285. } else {
  2286. /* the rest are all set to zero by kzalloc */
  2287. range->len = (u64)-1;
  2288. }
  2289. ret = btrfs_defrag_file(file_inode(file), file,
  2290. range, 0, 0);
  2291. if (ret > 0)
  2292. ret = 0;
  2293. kfree(range);
  2294. break;
  2295. default:
  2296. ret = -EINVAL;
  2297. }
  2298. out:
  2299. mnt_drop_write_file(file);
  2300. return ret;
  2301. }
  2302. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  2303. {
  2304. struct btrfs_ioctl_vol_args *vol_args;
  2305. int ret;
  2306. if (!capable(CAP_SYS_ADMIN))
  2307. return -EPERM;
  2308. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2309. 1)) {
  2310. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2311. }
  2312. mutex_lock(&root->fs_info->volume_mutex);
  2313. vol_args = memdup_user(arg, sizeof(*vol_args));
  2314. if (IS_ERR(vol_args)) {
  2315. ret = PTR_ERR(vol_args);
  2316. goto out;
  2317. }
  2318. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2319. ret = btrfs_init_new_device(root, vol_args->name);
  2320. if (!ret)
  2321. btrfs_info(root->fs_info, "disk added %s",vol_args->name);
  2322. kfree(vol_args);
  2323. out:
  2324. mutex_unlock(&root->fs_info->volume_mutex);
  2325. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2326. return ret;
  2327. }
  2328. static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
  2329. {
  2330. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2331. struct btrfs_ioctl_vol_args_v2 *vol_args;
  2332. int ret;
  2333. if (!capable(CAP_SYS_ADMIN))
  2334. return -EPERM;
  2335. ret = mnt_want_write_file(file);
  2336. if (ret)
  2337. return ret;
  2338. vol_args = memdup_user(arg, sizeof(*vol_args));
  2339. if (IS_ERR(vol_args)) {
  2340. ret = PTR_ERR(vol_args);
  2341. goto err_drop;
  2342. }
  2343. /* Check for compatibility reject unknown flags */
  2344. if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
  2345. return -EOPNOTSUPP;
  2346. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2347. 1)) {
  2348. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2349. goto out;
  2350. }
  2351. mutex_lock(&root->fs_info->volume_mutex);
  2352. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
  2353. ret = btrfs_rm_device(root, NULL, vol_args->devid);
  2354. } else {
  2355. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  2356. ret = btrfs_rm_device(root, vol_args->name, 0);
  2357. }
  2358. mutex_unlock(&root->fs_info->volume_mutex);
  2359. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2360. if (!ret) {
  2361. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
  2362. btrfs_info(root->fs_info, "device deleted: id %llu",
  2363. vol_args->devid);
  2364. else
  2365. btrfs_info(root->fs_info, "device deleted: %s",
  2366. vol_args->name);
  2367. }
  2368. out:
  2369. kfree(vol_args);
  2370. err_drop:
  2371. mnt_drop_write_file(file);
  2372. return ret;
  2373. }
  2374. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2375. {
  2376. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2377. struct btrfs_ioctl_vol_args *vol_args;
  2378. int ret;
  2379. if (!capable(CAP_SYS_ADMIN))
  2380. return -EPERM;
  2381. ret = mnt_want_write_file(file);
  2382. if (ret)
  2383. return ret;
  2384. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2385. 1)) {
  2386. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2387. goto out_drop_write;
  2388. }
  2389. vol_args = memdup_user(arg, sizeof(*vol_args));
  2390. if (IS_ERR(vol_args)) {
  2391. ret = PTR_ERR(vol_args);
  2392. goto out;
  2393. }
  2394. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2395. mutex_lock(&root->fs_info->volume_mutex);
  2396. ret = btrfs_rm_device(root, vol_args->name, 0);
  2397. mutex_unlock(&root->fs_info->volume_mutex);
  2398. if (!ret)
  2399. btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
  2400. kfree(vol_args);
  2401. out:
  2402. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2403. out_drop_write:
  2404. mnt_drop_write_file(file);
  2405. return ret;
  2406. }
  2407. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2408. {
  2409. struct btrfs_ioctl_fs_info_args *fi_args;
  2410. struct btrfs_device *device;
  2411. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2412. int ret = 0;
  2413. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2414. if (!fi_args)
  2415. return -ENOMEM;
  2416. mutex_lock(&fs_devices->device_list_mutex);
  2417. fi_args->num_devices = fs_devices->num_devices;
  2418. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2419. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  2420. if (device->devid > fi_args->max_id)
  2421. fi_args->max_id = device->devid;
  2422. }
  2423. mutex_unlock(&fs_devices->device_list_mutex);
  2424. fi_args->nodesize = root->fs_info->super_copy->nodesize;
  2425. fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
  2426. fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
  2427. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2428. ret = -EFAULT;
  2429. kfree(fi_args);
  2430. return ret;
  2431. }
  2432. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2433. {
  2434. struct btrfs_ioctl_dev_info_args *di_args;
  2435. struct btrfs_device *dev;
  2436. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2437. int ret = 0;
  2438. char *s_uuid = NULL;
  2439. di_args = memdup_user(arg, sizeof(*di_args));
  2440. if (IS_ERR(di_args))
  2441. return PTR_ERR(di_args);
  2442. if (!btrfs_is_empty_uuid(di_args->uuid))
  2443. s_uuid = di_args->uuid;
  2444. mutex_lock(&fs_devices->device_list_mutex);
  2445. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2446. if (!dev) {
  2447. ret = -ENODEV;
  2448. goto out;
  2449. }
  2450. di_args->devid = dev->devid;
  2451. di_args->bytes_used = btrfs_device_get_bytes_used(dev);
  2452. di_args->total_bytes = btrfs_device_get_total_bytes(dev);
  2453. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2454. if (dev->name) {
  2455. struct rcu_string *name;
  2456. rcu_read_lock();
  2457. name = rcu_dereference(dev->name);
  2458. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2459. rcu_read_unlock();
  2460. di_args->path[sizeof(di_args->path) - 1] = 0;
  2461. } else {
  2462. di_args->path[0] = '\0';
  2463. }
  2464. out:
  2465. mutex_unlock(&fs_devices->device_list_mutex);
  2466. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2467. ret = -EFAULT;
  2468. kfree(di_args);
  2469. return ret;
  2470. }
  2471. static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
  2472. {
  2473. struct page *page;
  2474. page = grab_cache_page(inode->i_mapping, index);
  2475. if (!page)
  2476. return ERR_PTR(-ENOMEM);
  2477. if (!PageUptodate(page)) {
  2478. int ret;
  2479. ret = btrfs_readpage(NULL, page);
  2480. if (ret)
  2481. return ERR_PTR(ret);
  2482. lock_page(page);
  2483. if (!PageUptodate(page)) {
  2484. unlock_page(page);
  2485. put_page(page);
  2486. return ERR_PTR(-EIO);
  2487. }
  2488. if (page->mapping != inode->i_mapping) {
  2489. unlock_page(page);
  2490. put_page(page);
  2491. return ERR_PTR(-EAGAIN);
  2492. }
  2493. }
  2494. return page;
  2495. }
  2496. static int gather_extent_pages(struct inode *inode, struct page **pages,
  2497. int num_pages, u64 off)
  2498. {
  2499. int i;
  2500. pgoff_t index = off >> PAGE_SHIFT;
  2501. for (i = 0; i < num_pages; i++) {
  2502. again:
  2503. pages[i] = extent_same_get_page(inode, index + i);
  2504. if (IS_ERR(pages[i])) {
  2505. int err = PTR_ERR(pages[i]);
  2506. if (err == -EAGAIN)
  2507. goto again;
  2508. pages[i] = NULL;
  2509. return err;
  2510. }
  2511. }
  2512. return 0;
  2513. }
  2514. static int lock_extent_range(struct inode *inode, u64 off, u64 len,
  2515. bool retry_range_locking)
  2516. {
  2517. /*
  2518. * Do any pending delalloc/csum calculations on inode, one way or
  2519. * another, and lock file content.
  2520. * The locking order is:
  2521. *
  2522. * 1) pages
  2523. * 2) range in the inode's io tree
  2524. */
  2525. while (1) {
  2526. struct btrfs_ordered_extent *ordered;
  2527. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2528. ordered = btrfs_lookup_first_ordered_extent(inode,
  2529. off + len - 1);
  2530. if ((!ordered ||
  2531. ordered->file_offset + ordered->len <= off ||
  2532. ordered->file_offset >= off + len) &&
  2533. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2534. off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
  2535. if (ordered)
  2536. btrfs_put_ordered_extent(ordered);
  2537. break;
  2538. }
  2539. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2540. if (ordered)
  2541. btrfs_put_ordered_extent(ordered);
  2542. if (!retry_range_locking)
  2543. return -EAGAIN;
  2544. btrfs_wait_ordered_range(inode, off, len);
  2545. }
  2546. return 0;
  2547. }
  2548. static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
  2549. {
  2550. inode_unlock(inode1);
  2551. inode_unlock(inode2);
  2552. }
  2553. static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
  2554. {
  2555. if (inode1 < inode2)
  2556. swap(inode1, inode2);
  2557. inode_lock_nested(inode1, I_MUTEX_PARENT);
  2558. inode_lock_nested(inode2, I_MUTEX_CHILD);
  2559. }
  2560. static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
  2561. struct inode *inode2, u64 loff2, u64 len)
  2562. {
  2563. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2564. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2565. }
  2566. static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
  2567. struct inode *inode2, u64 loff2, u64 len,
  2568. bool retry_range_locking)
  2569. {
  2570. int ret;
  2571. if (inode1 < inode2) {
  2572. swap(inode1, inode2);
  2573. swap(loff1, loff2);
  2574. }
  2575. ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
  2576. if (ret)
  2577. return ret;
  2578. ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
  2579. if (ret)
  2580. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
  2581. loff1 + len - 1);
  2582. return ret;
  2583. }
  2584. struct cmp_pages {
  2585. int num_pages;
  2586. struct page **src_pages;
  2587. struct page **dst_pages;
  2588. };
  2589. static void btrfs_cmp_data_free(struct cmp_pages *cmp)
  2590. {
  2591. int i;
  2592. struct page *pg;
  2593. for (i = 0; i < cmp->num_pages; i++) {
  2594. pg = cmp->src_pages[i];
  2595. if (pg) {
  2596. unlock_page(pg);
  2597. put_page(pg);
  2598. }
  2599. pg = cmp->dst_pages[i];
  2600. if (pg) {
  2601. unlock_page(pg);
  2602. put_page(pg);
  2603. }
  2604. }
  2605. kfree(cmp->src_pages);
  2606. kfree(cmp->dst_pages);
  2607. }
  2608. static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
  2609. struct inode *dst, u64 dst_loff,
  2610. u64 len, struct cmp_pages *cmp)
  2611. {
  2612. int ret;
  2613. int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
  2614. struct page **src_pgarr, **dst_pgarr;
  2615. /*
  2616. * We must gather up all the pages before we initiate our
  2617. * extent locking. We use an array for the page pointers. Size
  2618. * of the array is bounded by len, which is in turn bounded by
  2619. * BTRFS_MAX_DEDUPE_LEN.
  2620. */
  2621. src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
  2622. dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
  2623. if (!src_pgarr || !dst_pgarr) {
  2624. kfree(src_pgarr);
  2625. kfree(dst_pgarr);
  2626. return -ENOMEM;
  2627. }
  2628. cmp->num_pages = num_pages;
  2629. cmp->src_pages = src_pgarr;
  2630. cmp->dst_pages = dst_pgarr;
  2631. ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
  2632. if (ret)
  2633. goto out;
  2634. ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
  2635. out:
  2636. if (ret)
  2637. btrfs_cmp_data_free(cmp);
  2638. return ret;
  2639. }
  2640. static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
  2641. u64 dst_loff, u64 len, struct cmp_pages *cmp)
  2642. {
  2643. int ret = 0;
  2644. int i;
  2645. struct page *src_page, *dst_page;
  2646. unsigned int cmp_len = PAGE_SIZE;
  2647. void *addr, *dst_addr;
  2648. i = 0;
  2649. while (len) {
  2650. if (len < PAGE_SIZE)
  2651. cmp_len = len;
  2652. BUG_ON(i >= cmp->num_pages);
  2653. src_page = cmp->src_pages[i];
  2654. dst_page = cmp->dst_pages[i];
  2655. ASSERT(PageLocked(src_page));
  2656. ASSERT(PageLocked(dst_page));
  2657. addr = kmap_atomic(src_page);
  2658. dst_addr = kmap_atomic(dst_page);
  2659. flush_dcache_page(src_page);
  2660. flush_dcache_page(dst_page);
  2661. if (memcmp(addr, dst_addr, cmp_len))
  2662. ret = -EBADE;
  2663. kunmap_atomic(addr);
  2664. kunmap_atomic(dst_addr);
  2665. if (ret)
  2666. break;
  2667. len -= cmp_len;
  2668. i++;
  2669. }
  2670. return ret;
  2671. }
  2672. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
  2673. u64 olen)
  2674. {
  2675. u64 len = *plen;
  2676. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2677. if (off + olen > inode->i_size || off + olen < off)
  2678. return -EINVAL;
  2679. /* if we extend to eof, continue to block boundary */
  2680. if (off + len == inode->i_size)
  2681. *plen = len = ALIGN(inode->i_size, bs) - off;
  2682. /* Check that we are block aligned - btrfs_clone() requires this */
  2683. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2684. return -EINVAL;
  2685. return 0;
  2686. }
  2687. static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
  2688. struct inode *dst, u64 dst_loff)
  2689. {
  2690. int ret;
  2691. u64 len = olen;
  2692. struct cmp_pages cmp;
  2693. int same_inode = 0;
  2694. u64 same_lock_start = 0;
  2695. u64 same_lock_len = 0;
  2696. if (src == dst)
  2697. same_inode = 1;
  2698. if (len == 0)
  2699. return 0;
  2700. if (same_inode) {
  2701. inode_lock(src);
  2702. ret = extent_same_check_offsets(src, loff, &len, olen);
  2703. if (ret)
  2704. goto out_unlock;
  2705. ret = extent_same_check_offsets(src, dst_loff, &len, olen);
  2706. if (ret)
  2707. goto out_unlock;
  2708. /*
  2709. * Single inode case wants the same checks, except we
  2710. * don't want our length pushed out past i_size as
  2711. * comparing that data range makes no sense.
  2712. *
  2713. * extent_same_check_offsets() will do this for an
  2714. * unaligned length at i_size, so catch it here and
  2715. * reject the request.
  2716. *
  2717. * This effectively means we require aligned extents
  2718. * for the single-inode case, whereas the other cases
  2719. * allow an unaligned length so long as it ends at
  2720. * i_size.
  2721. */
  2722. if (len != olen) {
  2723. ret = -EINVAL;
  2724. goto out_unlock;
  2725. }
  2726. /* Check for overlapping ranges */
  2727. if (dst_loff + len > loff && dst_loff < loff + len) {
  2728. ret = -EINVAL;
  2729. goto out_unlock;
  2730. }
  2731. same_lock_start = min_t(u64, loff, dst_loff);
  2732. same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
  2733. } else {
  2734. btrfs_double_inode_lock(src, dst);
  2735. ret = extent_same_check_offsets(src, loff, &len, olen);
  2736. if (ret)
  2737. goto out_unlock;
  2738. ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
  2739. if (ret)
  2740. goto out_unlock;
  2741. }
  2742. /* don't make the dst file partly checksummed */
  2743. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2744. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  2745. ret = -EINVAL;
  2746. goto out_unlock;
  2747. }
  2748. again:
  2749. ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
  2750. if (ret)
  2751. goto out_unlock;
  2752. if (same_inode)
  2753. ret = lock_extent_range(src, same_lock_start, same_lock_len,
  2754. false);
  2755. else
  2756. ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
  2757. false);
  2758. /*
  2759. * If one of the inodes has dirty pages in the respective range or
  2760. * ordered extents, we need to flush dellaloc and wait for all ordered
  2761. * extents in the range. We must unlock the pages and the ranges in the
  2762. * io trees to avoid deadlocks when flushing delalloc (requires locking
  2763. * pages) and when waiting for ordered extents to complete (they require
  2764. * range locking).
  2765. */
  2766. if (ret == -EAGAIN) {
  2767. /*
  2768. * Ranges in the io trees already unlocked. Now unlock all
  2769. * pages before waiting for all IO to complete.
  2770. */
  2771. btrfs_cmp_data_free(&cmp);
  2772. if (same_inode) {
  2773. btrfs_wait_ordered_range(src, same_lock_start,
  2774. same_lock_len);
  2775. } else {
  2776. btrfs_wait_ordered_range(src, loff, len);
  2777. btrfs_wait_ordered_range(dst, dst_loff, len);
  2778. }
  2779. goto again;
  2780. }
  2781. ASSERT(ret == 0);
  2782. if (WARN_ON(ret)) {
  2783. /* ranges in the io trees already unlocked */
  2784. btrfs_cmp_data_free(&cmp);
  2785. return ret;
  2786. }
  2787. /* pass original length for comparison so we stay within i_size */
  2788. ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
  2789. if (ret == 0)
  2790. ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
  2791. if (same_inode)
  2792. unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
  2793. same_lock_start + same_lock_len - 1);
  2794. else
  2795. btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
  2796. btrfs_cmp_data_free(&cmp);
  2797. out_unlock:
  2798. if (same_inode)
  2799. inode_unlock(src);
  2800. else
  2801. btrfs_double_inode_unlock(src, dst);
  2802. return ret;
  2803. }
  2804. #define BTRFS_MAX_DEDUPE_LEN SZ_16M
  2805. ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
  2806. struct file *dst_file, u64 dst_loff)
  2807. {
  2808. struct inode *src = file_inode(src_file);
  2809. struct inode *dst = file_inode(dst_file);
  2810. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  2811. ssize_t res;
  2812. if (olen > BTRFS_MAX_DEDUPE_LEN)
  2813. olen = BTRFS_MAX_DEDUPE_LEN;
  2814. if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
  2815. /*
  2816. * Btrfs does not support blocksize < page_size. As a
  2817. * result, btrfs_cmp_data() won't correctly handle
  2818. * this situation without an update.
  2819. */
  2820. return -EINVAL;
  2821. }
  2822. res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
  2823. if (res)
  2824. return res;
  2825. return olen;
  2826. }
  2827. static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
  2828. struct inode *inode,
  2829. u64 endoff,
  2830. const u64 destoff,
  2831. const u64 olen,
  2832. int no_time_update)
  2833. {
  2834. struct btrfs_root *root = BTRFS_I(inode)->root;
  2835. int ret;
  2836. inode_inc_iversion(inode);
  2837. if (!no_time_update)
  2838. inode->i_mtime = inode->i_ctime = current_time(inode);
  2839. /*
  2840. * We round up to the block size at eof when determining which
  2841. * extents to clone above, but shouldn't round up the file size.
  2842. */
  2843. if (endoff > destoff + olen)
  2844. endoff = destoff + olen;
  2845. if (endoff > inode->i_size)
  2846. btrfs_i_size_write(inode, endoff);
  2847. ret = btrfs_update_inode(trans, root, inode);
  2848. if (ret) {
  2849. btrfs_abort_transaction(trans, ret);
  2850. btrfs_end_transaction(trans, root);
  2851. goto out;
  2852. }
  2853. ret = btrfs_end_transaction(trans, root);
  2854. out:
  2855. return ret;
  2856. }
  2857. static void clone_update_extent_map(struct inode *inode,
  2858. const struct btrfs_trans_handle *trans,
  2859. const struct btrfs_path *path,
  2860. const u64 hole_offset,
  2861. const u64 hole_len)
  2862. {
  2863. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2864. struct extent_map *em;
  2865. int ret;
  2866. em = alloc_extent_map();
  2867. if (!em) {
  2868. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2869. &BTRFS_I(inode)->runtime_flags);
  2870. return;
  2871. }
  2872. if (path) {
  2873. struct btrfs_file_extent_item *fi;
  2874. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2875. struct btrfs_file_extent_item);
  2876. btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
  2877. em->generation = -1;
  2878. if (btrfs_file_extent_type(path->nodes[0], fi) ==
  2879. BTRFS_FILE_EXTENT_INLINE)
  2880. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2881. &BTRFS_I(inode)->runtime_flags);
  2882. } else {
  2883. em->start = hole_offset;
  2884. em->len = hole_len;
  2885. em->ram_bytes = em->len;
  2886. em->orig_start = hole_offset;
  2887. em->block_start = EXTENT_MAP_HOLE;
  2888. em->block_len = 0;
  2889. em->orig_block_len = 0;
  2890. em->compress_type = BTRFS_COMPRESS_NONE;
  2891. em->generation = trans->transid;
  2892. }
  2893. while (1) {
  2894. write_lock(&em_tree->lock);
  2895. ret = add_extent_mapping(em_tree, em, 1);
  2896. write_unlock(&em_tree->lock);
  2897. if (ret != -EEXIST) {
  2898. free_extent_map(em);
  2899. break;
  2900. }
  2901. btrfs_drop_extent_cache(inode, em->start,
  2902. em->start + em->len - 1, 0);
  2903. }
  2904. if (ret)
  2905. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2906. &BTRFS_I(inode)->runtime_flags);
  2907. }
  2908. /*
  2909. * Make sure we do not end up inserting an inline extent into a file that has
  2910. * already other (non-inline) extents. If a file has an inline extent it can
  2911. * not have any other extents and the (single) inline extent must start at the
  2912. * file offset 0. Failing to respect these rules will lead to file corruption,
  2913. * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
  2914. *
  2915. * We can have extents that have been already written to disk or we can have
  2916. * dirty ranges still in delalloc, in which case the extent maps and items are
  2917. * created only when we run delalloc, and the delalloc ranges might fall outside
  2918. * the range we are currently locking in the inode's io tree. So we check the
  2919. * inode's i_size because of that (i_size updates are done while holding the
  2920. * i_mutex, which we are holding here).
  2921. * We also check to see if the inode has a size not greater than "datal" but has
  2922. * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
  2923. * protected against such concurrent fallocate calls by the i_mutex).
  2924. *
  2925. * If the file has no extents but a size greater than datal, do not allow the
  2926. * copy because we would need turn the inline extent into a non-inline one (even
  2927. * with NO_HOLES enabled). If we find our destination inode only has one inline
  2928. * extent, just overwrite it with the source inline extent if its size is less
  2929. * than the source extent's size, or we could copy the source inline extent's
  2930. * data into the destination inode's inline extent if the later is greater then
  2931. * the former.
  2932. */
  2933. static int clone_copy_inline_extent(struct inode *src,
  2934. struct inode *dst,
  2935. struct btrfs_trans_handle *trans,
  2936. struct btrfs_path *path,
  2937. struct btrfs_key *new_key,
  2938. const u64 drop_start,
  2939. const u64 datal,
  2940. const u64 skip,
  2941. const u64 size,
  2942. char *inline_data)
  2943. {
  2944. struct btrfs_root *root = BTRFS_I(dst)->root;
  2945. const u64 aligned_end = ALIGN(new_key->offset + datal,
  2946. root->sectorsize);
  2947. int ret;
  2948. struct btrfs_key key;
  2949. if (new_key->offset > 0)
  2950. return -EOPNOTSUPP;
  2951. key.objectid = btrfs_ino(dst);
  2952. key.type = BTRFS_EXTENT_DATA_KEY;
  2953. key.offset = 0;
  2954. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2955. if (ret < 0) {
  2956. return ret;
  2957. } else if (ret > 0) {
  2958. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  2959. ret = btrfs_next_leaf(root, path);
  2960. if (ret < 0)
  2961. return ret;
  2962. else if (ret > 0)
  2963. goto copy_inline_extent;
  2964. }
  2965. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  2966. if (key.objectid == btrfs_ino(dst) &&
  2967. key.type == BTRFS_EXTENT_DATA_KEY) {
  2968. ASSERT(key.offset > 0);
  2969. return -EOPNOTSUPP;
  2970. }
  2971. } else if (i_size_read(dst) <= datal) {
  2972. struct btrfs_file_extent_item *ei;
  2973. u64 ext_len;
  2974. /*
  2975. * If the file size is <= datal, make sure there are no other
  2976. * extents following (can happen do to an fallocate call with
  2977. * the flag FALLOC_FL_KEEP_SIZE).
  2978. */
  2979. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2980. struct btrfs_file_extent_item);
  2981. /*
  2982. * If it's an inline extent, it can not have other extents
  2983. * following it.
  2984. */
  2985. if (btrfs_file_extent_type(path->nodes[0], ei) ==
  2986. BTRFS_FILE_EXTENT_INLINE)
  2987. goto copy_inline_extent;
  2988. ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  2989. if (ext_len > aligned_end)
  2990. return -EOPNOTSUPP;
  2991. ret = btrfs_next_item(root, path);
  2992. if (ret < 0) {
  2993. return ret;
  2994. } else if (ret == 0) {
  2995. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2996. path->slots[0]);
  2997. if (key.objectid == btrfs_ino(dst) &&
  2998. key.type == BTRFS_EXTENT_DATA_KEY)
  2999. return -EOPNOTSUPP;
  3000. }
  3001. }
  3002. copy_inline_extent:
  3003. /*
  3004. * We have no extent items, or we have an extent at offset 0 which may
  3005. * or may not be inlined. All these cases are dealt the same way.
  3006. */
  3007. if (i_size_read(dst) > datal) {
  3008. /*
  3009. * If the destination inode has an inline extent...
  3010. * This would require copying the data from the source inline
  3011. * extent into the beginning of the destination's inline extent.
  3012. * But this is really complex, both extents can be compressed
  3013. * or just one of them, which would require decompressing and
  3014. * re-compressing data (which could increase the new compressed
  3015. * size, not allowing the compressed data to fit anymore in an
  3016. * inline extent).
  3017. * So just don't support this case for now (it should be rare,
  3018. * we are not really saving space when cloning inline extents).
  3019. */
  3020. return -EOPNOTSUPP;
  3021. }
  3022. btrfs_release_path(path);
  3023. ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
  3024. if (ret)
  3025. return ret;
  3026. ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
  3027. if (ret)
  3028. return ret;
  3029. if (skip) {
  3030. const u32 start = btrfs_file_extent_calc_inline_size(0);
  3031. memmove(inline_data + start, inline_data + start + skip, datal);
  3032. }
  3033. write_extent_buffer(path->nodes[0], inline_data,
  3034. btrfs_item_ptr_offset(path->nodes[0],
  3035. path->slots[0]),
  3036. size);
  3037. inode_add_bytes(dst, datal);
  3038. return 0;
  3039. }
  3040. /**
  3041. * btrfs_clone() - clone a range from inode file to another
  3042. *
  3043. * @src: Inode to clone from
  3044. * @inode: Inode to clone to
  3045. * @off: Offset within source to start clone from
  3046. * @olen: Original length, passed by user, of range to clone
  3047. * @olen_aligned: Block-aligned value of olen
  3048. * @destoff: Offset within @inode to start clone
  3049. * @no_time_update: Whether to update mtime/ctime on the target inode
  3050. */
  3051. static int btrfs_clone(struct inode *src, struct inode *inode,
  3052. const u64 off, const u64 olen, const u64 olen_aligned,
  3053. const u64 destoff, int no_time_update)
  3054. {
  3055. struct btrfs_root *root = BTRFS_I(inode)->root;
  3056. struct btrfs_path *path = NULL;
  3057. struct extent_buffer *leaf;
  3058. struct btrfs_trans_handle *trans;
  3059. char *buf = NULL;
  3060. struct btrfs_key key;
  3061. u32 nritems;
  3062. int slot;
  3063. int ret;
  3064. const u64 len = olen_aligned;
  3065. u64 last_dest_end = destoff;
  3066. ret = -ENOMEM;
  3067. buf = kmalloc(root->nodesize, GFP_KERNEL | __GFP_NOWARN);
  3068. if (!buf) {
  3069. buf = vmalloc(root->nodesize);
  3070. if (!buf)
  3071. return ret;
  3072. }
  3073. path = btrfs_alloc_path();
  3074. if (!path) {
  3075. kvfree(buf);
  3076. return ret;
  3077. }
  3078. path->reada = READA_FORWARD;
  3079. /* clone data */
  3080. key.objectid = btrfs_ino(src);
  3081. key.type = BTRFS_EXTENT_DATA_KEY;
  3082. key.offset = off;
  3083. while (1) {
  3084. u64 next_key_min_offset = key.offset + 1;
  3085. /*
  3086. * note the key will change type as we walk through the
  3087. * tree.
  3088. */
  3089. path->leave_spinning = 1;
  3090. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  3091. 0, 0);
  3092. if (ret < 0)
  3093. goto out;
  3094. /*
  3095. * First search, if no extent item that starts at offset off was
  3096. * found but the previous item is an extent item, it's possible
  3097. * it might overlap our target range, therefore process it.
  3098. */
  3099. if (key.offset == off && ret > 0 && path->slots[0] > 0) {
  3100. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3101. path->slots[0] - 1);
  3102. if (key.type == BTRFS_EXTENT_DATA_KEY)
  3103. path->slots[0]--;
  3104. }
  3105. nritems = btrfs_header_nritems(path->nodes[0]);
  3106. process_slot:
  3107. if (path->slots[0] >= nritems) {
  3108. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  3109. if (ret < 0)
  3110. goto out;
  3111. if (ret > 0)
  3112. break;
  3113. nritems = btrfs_header_nritems(path->nodes[0]);
  3114. }
  3115. leaf = path->nodes[0];
  3116. slot = path->slots[0];
  3117. btrfs_item_key_to_cpu(leaf, &key, slot);
  3118. if (key.type > BTRFS_EXTENT_DATA_KEY ||
  3119. key.objectid != btrfs_ino(src))
  3120. break;
  3121. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3122. struct btrfs_file_extent_item *extent;
  3123. int type;
  3124. u32 size;
  3125. struct btrfs_key new_key;
  3126. u64 disko = 0, diskl = 0;
  3127. u64 datao = 0, datal = 0;
  3128. u8 comp;
  3129. u64 drop_start;
  3130. extent = btrfs_item_ptr(leaf, slot,
  3131. struct btrfs_file_extent_item);
  3132. comp = btrfs_file_extent_compression(leaf, extent);
  3133. type = btrfs_file_extent_type(leaf, extent);
  3134. if (type == BTRFS_FILE_EXTENT_REG ||
  3135. type == BTRFS_FILE_EXTENT_PREALLOC) {
  3136. disko = btrfs_file_extent_disk_bytenr(leaf,
  3137. extent);
  3138. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  3139. extent);
  3140. datao = btrfs_file_extent_offset(leaf, extent);
  3141. datal = btrfs_file_extent_num_bytes(leaf,
  3142. extent);
  3143. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  3144. /* take upper bound, may be compressed */
  3145. datal = btrfs_file_extent_ram_bytes(leaf,
  3146. extent);
  3147. }
  3148. /*
  3149. * The first search might have left us at an extent
  3150. * item that ends before our target range's start, can
  3151. * happen if we have holes and NO_HOLES feature enabled.
  3152. */
  3153. if (key.offset + datal <= off) {
  3154. path->slots[0]++;
  3155. goto process_slot;
  3156. } else if (key.offset >= off + len) {
  3157. break;
  3158. }
  3159. next_key_min_offset = key.offset + datal;
  3160. size = btrfs_item_size_nr(leaf, slot);
  3161. read_extent_buffer(leaf, buf,
  3162. btrfs_item_ptr_offset(leaf, slot),
  3163. size);
  3164. btrfs_release_path(path);
  3165. path->leave_spinning = 0;
  3166. memcpy(&new_key, &key, sizeof(new_key));
  3167. new_key.objectid = btrfs_ino(inode);
  3168. if (off <= key.offset)
  3169. new_key.offset = key.offset + destoff - off;
  3170. else
  3171. new_key.offset = destoff;
  3172. /*
  3173. * Deal with a hole that doesn't have an extent item
  3174. * that represents it (NO_HOLES feature enabled).
  3175. * This hole is either in the middle of the cloning
  3176. * range or at the beginning (fully overlaps it or
  3177. * partially overlaps it).
  3178. */
  3179. if (new_key.offset != last_dest_end)
  3180. drop_start = last_dest_end;
  3181. else
  3182. drop_start = new_key.offset;
  3183. /*
  3184. * 1 - adjusting old extent (we may have to split it)
  3185. * 1 - add new extent
  3186. * 1 - inode update
  3187. */
  3188. trans = btrfs_start_transaction(root, 3);
  3189. if (IS_ERR(trans)) {
  3190. ret = PTR_ERR(trans);
  3191. goto out;
  3192. }
  3193. if (type == BTRFS_FILE_EXTENT_REG ||
  3194. type == BTRFS_FILE_EXTENT_PREALLOC) {
  3195. /*
  3196. * a | --- range to clone ---| b
  3197. * | ------------- extent ------------- |
  3198. */
  3199. /* subtract range b */
  3200. if (key.offset + datal > off + len)
  3201. datal = off + len - key.offset;
  3202. /* subtract range a */
  3203. if (off > key.offset) {
  3204. datao += off - key.offset;
  3205. datal -= off - key.offset;
  3206. }
  3207. ret = btrfs_drop_extents(trans, root, inode,
  3208. drop_start,
  3209. new_key.offset + datal,
  3210. 1);
  3211. if (ret) {
  3212. if (ret != -EOPNOTSUPP)
  3213. btrfs_abort_transaction(trans,
  3214. ret);
  3215. btrfs_end_transaction(trans, root);
  3216. goto out;
  3217. }
  3218. ret = btrfs_insert_empty_item(trans, root, path,
  3219. &new_key, size);
  3220. if (ret) {
  3221. btrfs_abort_transaction(trans, ret);
  3222. btrfs_end_transaction(trans, root);
  3223. goto out;
  3224. }
  3225. leaf = path->nodes[0];
  3226. slot = path->slots[0];
  3227. write_extent_buffer(leaf, buf,
  3228. btrfs_item_ptr_offset(leaf, slot),
  3229. size);
  3230. extent = btrfs_item_ptr(leaf, slot,
  3231. struct btrfs_file_extent_item);
  3232. /* disko == 0 means it's a hole */
  3233. if (!disko)
  3234. datao = 0;
  3235. btrfs_set_file_extent_offset(leaf, extent,
  3236. datao);
  3237. btrfs_set_file_extent_num_bytes(leaf, extent,
  3238. datal);
  3239. if (disko) {
  3240. inode_add_bytes(inode, datal);
  3241. ret = btrfs_inc_extent_ref(trans, root,
  3242. disko, diskl, 0,
  3243. root->root_key.objectid,
  3244. btrfs_ino(inode),
  3245. new_key.offset - datao);
  3246. if (ret) {
  3247. btrfs_abort_transaction(trans,
  3248. ret);
  3249. btrfs_end_transaction(trans,
  3250. root);
  3251. goto out;
  3252. }
  3253. }
  3254. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  3255. u64 skip = 0;
  3256. u64 trim = 0;
  3257. if (off > key.offset) {
  3258. skip = off - key.offset;
  3259. new_key.offset += skip;
  3260. }
  3261. if (key.offset + datal > off + len)
  3262. trim = key.offset + datal - (off + len);
  3263. if (comp && (skip || trim)) {
  3264. ret = -EINVAL;
  3265. btrfs_end_transaction(trans, root);
  3266. goto out;
  3267. }
  3268. size -= skip + trim;
  3269. datal -= skip + trim;
  3270. ret = clone_copy_inline_extent(src, inode,
  3271. trans, path,
  3272. &new_key,
  3273. drop_start,
  3274. datal,
  3275. skip, size, buf);
  3276. if (ret) {
  3277. if (ret != -EOPNOTSUPP)
  3278. btrfs_abort_transaction(trans,
  3279. ret);
  3280. btrfs_end_transaction(trans, root);
  3281. goto out;
  3282. }
  3283. leaf = path->nodes[0];
  3284. slot = path->slots[0];
  3285. }
  3286. /* If we have an implicit hole (NO_HOLES feature). */
  3287. if (drop_start < new_key.offset)
  3288. clone_update_extent_map(inode, trans,
  3289. NULL, drop_start,
  3290. new_key.offset - drop_start);
  3291. clone_update_extent_map(inode, trans, path, 0, 0);
  3292. btrfs_mark_buffer_dirty(leaf);
  3293. btrfs_release_path(path);
  3294. last_dest_end = ALIGN(new_key.offset + datal,
  3295. root->sectorsize);
  3296. ret = clone_finish_inode_update(trans, inode,
  3297. last_dest_end,
  3298. destoff, olen,
  3299. no_time_update);
  3300. if (ret)
  3301. goto out;
  3302. if (new_key.offset + datal >= destoff + len)
  3303. break;
  3304. }
  3305. btrfs_release_path(path);
  3306. key.offset = next_key_min_offset;
  3307. if (fatal_signal_pending(current)) {
  3308. ret = -EINTR;
  3309. goto out;
  3310. }
  3311. }
  3312. ret = 0;
  3313. if (last_dest_end < destoff + len) {
  3314. /*
  3315. * We have an implicit hole (NO_HOLES feature is enabled) that
  3316. * fully or partially overlaps our cloning range at its end.
  3317. */
  3318. btrfs_release_path(path);
  3319. /*
  3320. * 1 - remove extent(s)
  3321. * 1 - inode update
  3322. */
  3323. trans = btrfs_start_transaction(root, 2);
  3324. if (IS_ERR(trans)) {
  3325. ret = PTR_ERR(trans);
  3326. goto out;
  3327. }
  3328. ret = btrfs_drop_extents(trans, root, inode,
  3329. last_dest_end, destoff + len, 1);
  3330. if (ret) {
  3331. if (ret != -EOPNOTSUPP)
  3332. btrfs_abort_transaction(trans, ret);
  3333. btrfs_end_transaction(trans, root);
  3334. goto out;
  3335. }
  3336. clone_update_extent_map(inode, trans, NULL, last_dest_end,
  3337. destoff + len - last_dest_end);
  3338. ret = clone_finish_inode_update(trans, inode, destoff + len,
  3339. destoff, olen, no_time_update);
  3340. }
  3341. out:
  3342. btrfs_free_path(path);
  3343. kvfree(buf);
  3344. return ret;
  3345. }
  3346. static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  3347. u64 off, u64 olen, u64 destoff)
  3348. {
  3349. struct inode *inode = file_inode(file);
  3350. struct inode *src = file_inode(file_src);
  3351. struct btrfs_root *root = BTRFS_I(inode)->root;
  3352. int ret;
  3353. u64 len = olen;
  3354. u64 bs = root->fs_info->sb->s_blocksize;
  3355. int same_inode = src == inode;
  3356. /*
  3357. * TODO:
  3358. * - split compressed inline extents. annoying: we need to
  3359. * decompress into destination's address_space (the file offset
  3360. * may change, so source mapping won't do), then recompress (or
  3361. * otherwise reinsert) a subrange.
  3362. *
  3363. * - split destination inode's inline extents. The inline extents can
  3364. * be either compressed or non-compressed.
  3365. */
  3366. if (btrfs_root_readonly(root))
  3367. return -EROFS;
  3368. if (file_src->f_path.mnt != file->f_path.mnt ||
  3369. src->i_sb != inode->i_sb)
  3370. return -EXDEV;
  3371. /* don't make the dst file partly checksummed */
  3372. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3373. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  3374. return -EINVAL;
  3375. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  3376. return -EISDIR;
  3377. if (!same_inode) {
  3378. btrfs_double_inode_lock(src, inode);
  3379. } else {
  3380. inode_lock(src);
  3381. }
  3382. /* determine range to clone */
  3383. ret = -EINVAL;
  3384. if (off + len > src->i_size || off + len < off)
  3385. goto out_unlock;
  3386. if (len == 0)
  3387. olen = len = src->i_size - off;
  3388. /* if we extend to eof, continue to block boundary */
  3389. if (off + len == src->i_size)
  3390. len = ALIGN(src->i_size, bs) - off;
  3391. if (len == 0) {
  3392. ret = 0;
  3393. goto out_unlock;
  3394. }
  3395. /* verify the end result is block aligned */
  3396. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  3397. !IS_ALIGNED(destoff, bs))
  3398. goto out_unlock;
  3399. /* verify if ranges are overlapped within the same file */
  3400. if (same_inode) {
  3401. if (destoff + len > off && destoff < off + len)
  3402. goto out_unlock;
  3403. }
  3404. if (destoff > inode->i_size) {
  3405. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  3406. if (ret)
  3407. goto out_unlock;
  3408. }
  3409. /*
  3410. * Lock the target range too. Right after we replace the file extent
  3411. * items in the fs tree (which now point to the cloned data), we might
  3412. * have a worker replace them with extent items relative to a write
  3413. * operation that was issued before this clone operation (i.e. confront
  3414. * with inode.c:btrfs_finish_ordered_io).
  3415. */
  3416. if (same_inode) {
  3417. u64 lock_start = min_t(u64, off, destoff);
  3418. u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
  3419. ret = lock_extent_range(src, lock_start, lock_len, true);
  3420. } else {
  3421. ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
  3422. true);
  3423. }
  3424. ASSERT(ret == 0);
  3425. if (WARN_ON(ret)) {
  3426. /* ranges in the io trees already unlocked */
  3427. goto out_unlock;
  3428. }
  3429. ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
  3430. if (same_inode) {
  3431. u64 lock_start = min_t(u64, off, destoff);
  3432. u64 lock_end = max_t(u64, off, destoff) + len - 1;
  3433. unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
  3434. } else {
  3435. btrfs_double_extent_unlock(src, off, inode, destoff, len);
  3436. }
  3437. /*
  3438. * Truncate page cache pages so that future reads will see the cloned
  3439. * data immediately and not the previous data.
  3440. */
  3441. truncate_inode_pages_range(&inode->i_data,
  3442. round_down(destoff, PAGE_SIZE),
  3443. round_up(destoff + len, PAGE_SIZE) - 1);
  3444. out_unlock:
  3445. if (!same_inode)
  3446. btrfs_double_inode_unlock(src, inode);
  3447. else
  3448. inode_unlock(src);
  3449. return ret;
  3450. }
  3451. ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
  3452. struct file *file_out, loff_t pos_out,
  3453. size_t len, unsigned int flags)
  3454. {
  3455. ssize_t ret;
  3456. ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
  3457. if (ret == 0)
  3458. ret = len;
  3459. return ret;
  3460. }
  3461. int btrfs_clone_file_range(struct file *src_file, loff_t off,
  3462. struct file *dst_file, loff_t destoff, u64 len)
  3463. {
  3464. return btrfs_clone_files(dst_file, src_file, off, len, destoff);
  3465. }
  3466. /*
  3467. * there are many ways the trans_start and trans_end ioctls can lead
  3468. * to deadlocks. They should only be used by applications that
  3469. * basically own the machine, and have a very in depth understanding
  3470. * of all the possible deadlocks and enospc problems.
  3471. */
  3472. static long btrfs_ioctl_trans_start(struct file *file)
  3473. {
  3474. struct inode *inode = file_inode(file);
  3475. struct btrfs_root *root = BTRFS_I(inode)->root;
  3476. struct btrfs_trans_handle *trans;
  3477. int ret;
  3478. ret = -EPERM;
  3479. if (!capable(CAP_SYS_ADMIN))
  3480. goto out;
  3481. ret = -EINPROGRESS;
  3482. if (file->private_data)
  3483. goto out;
  3484. ret = -EROFS;
  3485. if (btrfs_root_readonly(root))
  3486. goto out;
  3487. ret = mnt_want_write_file(file);
  3488. if (ret)
  3489. goto out;
  3490. atomic_inc(&root->fs_info->open_ioctl_trans);
  3491. ret = -ENOMEM;
  3492. trans = btrfs_start_ioctl_transaction(root);
  3493. if (IS_ERR(trans))
  3494. goto out_drop;
  3495. file->private_data = trans;
  3496. return 0;
  3497. out_drop:
  3498. atomic_dec(&root->fs_info->open_ioctl_trans);
  3499. mnt_drop_write_file(file);
  3500. out:
  3501. return ret;
  3502. }
  3503. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  3504. {
  3505. struct inode *inode = file_inode(file);
  3506. struct btrfs_root *root = BTRFS_I(inode)->root;
  3507. struct btrfs_root *new_root;
  3508. struct btrfs_dir_item *di;
  3509. struct btrfs_trans_handle *trans;
  3510. struct btrfs_path *path;
  3511. struct btrfs_key location;
  3512. struct btrfs_disk_key disk_key;
  3513. u64 objectid = 0;
  3514. u64 dir_id;
  3515. int ret;
  3516. if (!capable(CAP_SYS_ADMIN))
  3517. return -EPERM;
  3518. ret = mnt_want_write_file(file);
  3519. if (ret)
  3520. return ret;
  3521. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  3522. ret = -EFAULT;
  3523. goto out;
  3524. }
  3525. if (!objectid)
  3526. objectid = BTRFS_FS_TREE_OBJECTID;
  3527. location.objectid = objectid;
  3528. location.type = BTRFS_ROOT_ITEM_KEY;
  3529. location.offset = (u64)-1;
  3530. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  3531. if (IS_ERR(new_root)) {
  3532. ret = PTR_ERR(new_root);
  3533. goto out;
  3534. }
  3535. if (!is_fstree(new_root->objectid)) {
  3536. ret = -ENOENT;
  3537. goto out;
  3538. }
  3539. path = btrfs_alloc_path();
  3540. if (!path) {
  3541. ret = -ENOMEM;
  3542. goto out;
  3543. }
  3544. path->leave_spinning = 1;
  3545. trans = btrfs_start_transaction(root, 1);
  3546. if (IS_ERR(trans)) {
  3547. btrfs_free_path(path);
  3548. ret = PTR_ERR(trans);
  3549. goto out;
  3550. }
  3551. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  3552. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  3553. dir_id, "default", 7, 1);
  3554. if (IS_ERR_OR_NULL(di)) {
  3555. btrfs_free_path(path);
  3556. btrfs_end_transaction(trans, root);
  3557. btrfs_err(new_root->fs_info,
  3558. "Umm, you don't have the default diritem, this isn't going to work");
  3559. ret = -ENOENT;
  3560. goto out;
  3561. }
  3562. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  3563. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  3564. btrfs_mark_buffer_dirty(path->nodes[0]);
  3565. btrfs_free_path(path);
  3566. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  3567. btrfs_end_transaction(trans, root);
  3568. out:
  3569. mnt_drop_write_file(file);
  3570. return ret;
  3571. }
  3572. void btrfs_get_block_group_info(struct list_head *groups_list,
  3573. struct btrfs_ioctl_space_info *space)
  3574. {
  3575. struct btrfs_block_group_cache *block_group;
  3576. space->total_bytes = 0;
  3577. space->used_bytes = 0;
  3578. space->flags = 0;
  3579. list_for_each_entry(block_group, groups_list, list) {
  3580. space->flags = block_group->flags;
  3581. space->total_bytes += block_group->key.offset;
  3582. space->used_bytes +=
  3583. btrfs_block_group_used(&block_group->item);
  3584. }
  3585. }
  3586. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  3587. {
  3588. struct btrfs_ioctl_space_args space_args;
  3589. struct btrfs_ioctl_space_info space;
  3590. struct btrfs_ioctl_space_info *dest;
  3591. struct btrfs_ioctl_space_info *dest_orig;
  3592. struct btrfs_ioctl_space_info __user *user_dest;
  3593. struct btrfs_space_info *info;
  3594. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  3595. BTRFS_BLOCK_GROUP_SYSTEM,
  3596. BTRFS_BLOCK_GROUP_METADATA,
  3597. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  3598. int num_types = 4;
  3599. int alloc_size;
  3600. int ret = 0;
  3601. u64 slot_count = 0;
  3602. int i, c;
  3603. if (copy_from_user(&space_args,
  3604. (struct btrfs_ioctl_space_args __user *)arg,
  3605. sizeof(space_args)))
  3606. return -EFAULT;
  3607. for (i = 0; i < num_types; i++) {
  3608. struct btrfs_space_info *tmp;
  3609. info = NULL;
  3610. rcu_read_lock();
  3611. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3612. list) {
  3613. if (tmp->flags == types[i]) {
  3614. info = tmp;
  3615. break;
  3616. }
  3617. }
  3618. rcu_read_unlock();
  3619. if (!info)
  3620. continue;
  3621. down_read(&info->groups_sem);
  3622. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3623. if (!list_empty(&info->block_groups[c]))
  3624. slot_count++;
  3625. }
  3626. up_read(&info->groups_sem);
  3627. }
  3628. /*
  3629. * Global block reserve, exported as a space_info
  3630. */
  3631. slot_count++;
  3632. /* space_slots == 0 means they are asking for a count */
  3633. if (space_args.space_slots == 0) {
  3634. space_args.total_spaces = slot_count;
  3635. goto out;
  3636. }
  3637. slot_count = min_t(u64, space_args.space_slots, slot_count);
  3638. alloc_size = sizeof(*dest) * slot_count;
  3639. /* we generally have at most 6 or so space infos, one for each raid
  3640. * level. So, a whole page should be more than enough for everyone
  3641. */
  3642. if (alloc_size > PAGE_SIZE)
  3643. return -ENOMEM;
  3644. space_args.total_spaces = 0;
  3645. dest = kmalloc(alloc_size, GFP_KERNEL);
  3646. if (!dest)
  3647. return -ENOMEM;
  3648. dest_orig = dest;
  3649. /* now we have a buffer to copy into */
  3650. for (i = 0; i < num_types; i++) {
  3651. struct btrfs_space_info *tmp;
  3652. if (!slot_count)
  3653. break;
  3654. info = NULL;
  3655. rcu_read_lock();
  3656. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3657. list) {
  3658. if (tmp->flags == types[i]) {
  3659. info = tmp;
  3660. break;
  3661. }
  3662. }
  3663. rcu_read_unlock();
  3664. if (!info)
  3665. continue;
  3666. down_read(&info->groups_sem);
  3667. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3668. if (!list_empty(&info->block_groups[c])) {
  3669. btrfs_get_block_group_info(
  3670. &info->block_groups[c], &space);
  3671. memcpy(dest, &space, sizeof(space));
  3672. dest++;
  3673. space_args.total_spaces++;
  3674. slot_count--;
  3675. }
  3676. if (!slot_count)
  3677. break;
  3678. }
  3679. up_read(&info->groups_sem);
  3680. }
  3681. /*
  3682. * Add global block reserve
  3683. */
  3684. if (slot_count) {
  3685. struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
  3686. spin_lock(&block_rsv->lock);
  3687. space.total_bytes = block_rsv->size;
  3688. space.used_bytes = block_rsv->size - block_rsv->reserved;
  3689. spin_unlock(&block_rsv->lock);
  3690. space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
  3691. memcpy(dest, &space, sizeof(space));
  3692. space_args.total_spaces++;
  3693. }
  3694. user_dest = (struct btrfs_ioctl_space_info __user *)
  3695. (arg + sizeof(struct btrfs_ioctl_space_args));
  3696. if (copy_to_user(user_dest, dest_orig, alloc_size))
  3697. ret = -EFAULT;
  3698. kfree(dest_orig);
  3699. out:
  3700. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  3701. ret = -EFAULT;
  3702. return ret;
  3703. }
  3704. /*
  3705. * there are many ways the trans_start and trans_end ioctls can lead
  3706. * to deadlocks. They should only be used by applications that
  3707. * basically own the machine, and have a very in depth understanding
  3708. * of all the possible deadlocks and enospc problems.
  3709. */
  3710. long btrfs_ioctl_trans_end(struct file *file)
  3711. {
  3712. struct inode *inode = file_inode(file);
  3713. struct btrfs_root *root = BTRFS_I(inode)->root;
  3714. struct btrfs_trans_handle *trans;
  3715. trans = file->private_data;
  3716. if (!trans)
  3717. return -EINVAL;
  3718. file->private_data = NULL;
  3719. btrfs_end_transaction(trans, root);
  3720. atomic_dec(&root->fs_info->open_ioctl_trans);
  3721. mnt_drop_write_file(file);
  3722. return 0;
  3723. }
  3724. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  3725. void __user *argp)
  3726. {
  3727. struct btrfs_trans_handle *trans;
  3728. u64 transid;
  3729. int ret;
  3730. trans = btrfs_attach_transaction_barrier(root);
  3731. if (IS_ERR(trans)) {
  3732. if (PTR_ERR(trans) != -ENOENT)
  3733. return PTR_ERR(trans);
  3734. /* No running transaction, don't bother */
  3735. transid = root->fs_info->last_trans_committed;
  3736. goto out;
  3737. }
  3738. transid = trans->transid;
  3739. ret = btrfs_commit_transaction_async(trans, root, 0);
  3740. if (ret) {
  3741. btrfs_end_transaction(trans, root);
  3742. return ret;
  3743. }
  3744. out:
  3745. if (argp)
  3746. if (copy_to_user(argp, &transid, sizeof(transid)))
  3747. return -EFAULT;
  3748. return 0;
  3749. }
  3750. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  3751. void __user *argp)
  3752. {
  3753. u64 transid;
  3754. if (argp) {
  3755. if (copy_from_user(&transid, argp, sizeof(transid)))
  3756. return -EFAULT;
  3757. } else {
  3758. transid = 0; /* current trans */
  3759. }
  3760. return btrfs_wait_for_commit(root, transid);
  3761. }
  3762. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  3763. {
  3764. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3765. struct btrfs_ioctl_scrub_args *sa;
  3766. int ret;
  3767. if (!capable(CAP_SYS_ADMIN))
  3768. return -EPERM;
  3769. sa = memdup_user(arg, sizeof(*sa));
  3770. if (IS_ERR(sa))
  3771. return PTR_ERR(sa);
  3772. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  3773. ret = mnt_want_write_file(file);
  3774. if (ret)
  3775. goto out;
  3776. }
  3777. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  3778. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  3779. 0);
  3780. if (copy_to_user(arg, sa, sizeof(*sa)))
  3781. ret = -EFAULT;
  3782. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  3783. mnt_drop_write_file(file);
  3784. out:
  3785. kfree(sa);
  3786. return ret;
  3787. }
  3788. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  3789. {
  3790. if (!capable(CAP_SYS_ADMIN))
  3791. return -EPERM;
  3792. return btrfs_scrub_cancel(root->fs_info);
  3793. }
  3794. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  3795. void __user *arg)
  3796. {
  3797. struct btrfs_ioctl_scrub_args *sa;
  3798. int ret;
  3799. if (!capable(CAP_SYS_ADMIN))
  3800. return -EPERM;
  3801. sa = memdup_user(arg, sizeof(*sa));
  3802. if (IS_ERR(sa))
  3803. return PTR_ERR(sa);
  3804. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  3805. if (copy_to_user(arg, sa, sizeof(*sa)))
  3806. ret = -EFAULT;
  3807. kfree(sa);
  3808. return ret;
  3809. }
  3810. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  3811. void __user *arg)
  3812. {
  3813. struct btrfs_ioctl_get_dev_stats *sa;
  3814. int ret;
  3815. sa = memdup_user(arg, sizeof(*sa));
  3816. if (IS_ERR(sa))
  3817. return PTR_ERR(sa);
  3818. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  3819. kfree(sa);
  3820. return -EPERM;
  3821. }
  3822. ret = btrfs_get_dev_stats(root, sa);
  3823. if (copy_to_user(arg, sa, sizeof(*sa)))
  3824. ret = -EFAULT;
  3825. kfree(sa);
  3826. return ret;
  3827. }
  3828. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  3829. {
  3830. struct btrfs_ioctl_dev_replace_args *p;
  3831. int ret;
  3832. if (!capable(CAP_SYS_ADMIN))
  3833. return -EPERM;
  3834. p = memdup_user(arg, sizeof(*p));
  3835. if (IS_ERR(p))
  3836. return PTR_ERR(p);
  3837. switch (p->cmd) {
  3838. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  3839. if (root->fs_info->sb->s_flags & MS_RDONLY) {
  3840. ret = -EROFS;
  3841. goto out;
  3842. }
  3843. if (atomic_xchg(
  3844. &root->fs_info->mutually_exclusive_operation_running,
  3845. 1)) {
  3846. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3847. } else {
  3848. ret = btrfs_dev_replace_by_ioctl(root, p);
  3849. atomic_set(
  3850. &root->fs_info->mutually_exclusive_operation_running,
  3851. 0);
  3852. }
  3853. break;
  3854. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  3855. btrfs_dev_replace_status(root->fs_info, p);
  3856. ret = 0;
  3857. break;
  3858. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  3859. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  3860. break;
  3861. default:
  3862. ret = -EINVAL;
  3863. break;
  3864. }
  3865. if (copy_to_user(arg, p, sizeof(*p)))
  3866. ret = -EFAULT;
  3867. out:
  3868. kfree(p);
  3869. return ret;
  3870. }
  3871. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  3872. {
  3873. int ret = 0;
  3874. int i;
  3875. u64 rel_ptr;
  3876. int size;
  3877. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  3878. struct inode_fs_paths *ipath = NULL;
  3879. struct btrfs_path *path;
  3880. if (!capable(CAP_DAC_READ_SEARCH))
  3881. return -EPERM;
  3882. path = btrfs_alloc_path();
  3883. if (!path) {
  3884. ret = -ENOMEM;
  3885. goto out;
  3886. }
  3887. ipa = memdup_user(arg, sizeof(*ipa));
  3888. if (IS_ERR(ipa)) {
  3889. ret = PTR_ERR(ipa);
  3890. ipa = NULL;
  3891. goto out;
  3892. }
  3893. size = min_t(u32, ipa->size, 4096);
  3894. ipath = init_ipath(size, root, path);
  3895. if (IS_ERR(ipath)) {
  3896. ret = PTR_ERR(ipath);
  3897. ipath = NULL;
  3898. goto out;
  3899. }
  3900. ret = paths_from_inode(ipa->inum, ipath);
  3901. if (ret < 0)
  3902. goto out;
  3903. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  3904. rel_ptr = ipath->fspath->val[i] -
  3905. (u64)(unsigned long)ipath->fspath->val;
  3906. ipath->fspath->val[i] = rel_ptr;
  3907. }
  3908. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  3909. (void *)(unsigned long)ipath->fspath, size);
  3910. if (ret) {
  3911. ret = -EFAULT;
  3912. goto out;
  3913. }
  3914. out:
  3915. btrfs_free_path(path);
  3916. free_ipath(ipath);
  3917. kfree(ipa);
  3918. return ret;
  3919. }
  3920. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  3921. {
  3922. struct btrfs_data_container *inodes = ctx;
  3923. const size_t c = 3 * sizeof(u64);
  3924. if (inodes->bytes_left >= c) {
  3925. inodes->bytes_left -= c;
  3926. inodes->val[inodes->elem_cnt] = inum;
  3927. inodes->val[inodes->elem_cnt + 1] = offset;
  3928. inodes->val[inodes->elem_cnt + 2] = root;
  3929. inodes->elem_cnt += 3;
  3930. } else {
  3931. inodes->bytes_missing += c - inodes->bytes_left;
  3932. inodes->bytes_left = 0;
  3933. inodes->elem_missed += 3;
  3934. }
  3935. return 0;
  3936. }
  3937. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  3938. void __user *arg)
  3939. {
  3940. int ret = 0;
  3941. int size;
  3942. struct btrfs_ioctl_logical_ino_args *loi;
  3943. struct btrfs_data_container *inodes = NULL;
  3944. struct btrfs_path *path = NULL;
  3945. if (!capable(CAP_SYS_ADMIN))
  3946. return -EPERM;
  3947. loi = memdup_user(arg, sizeof(*loi));
  3948. if (IS_ERR(loi)) {
  3949. ret = PTR_ERR(loi);
  3950. loi = NULL;
  3951. goto out;
  3952. }
  3953. path = btrfs_alloc_path();
  3954. if (!path) {
  3955. ret = -ENOMEM;
  3956. goto out;
  3957. }
  3958. size = min_t(u32, loi->size, SZ_64K);
  3959. inodes = init_data_container(size);
  3960. if (IS_ERR(inodes)) {
  3961. ret = PTR_ERR(inodes);
  3962. inodes = NULL;
  3963. goto out;
  3964. }
  3965. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  3966. build_ino_list, inodes);
  3967. if (ret == -EINVAL)
  3968. ret = -ENOENT;
  3969. if (ret < 0)
  3970. goto out;
  3971. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  3972. (void *)(unsigned long)inodes, size);
  3973. if (ret)
  3974. ret = -EFAULT;
  3975. out:
  3976. btrfs_free_path(path);
  3977. vfree(inodes);
  3978. kfree(loi);
  3979. return ret;
  3980. }
  3981. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  3982. struct btrfs_ioctl_balance_args *bargs)
  3983. {
  3984. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3985. bargs->flags = bctl->flags;
  3986. if (atomic_read(&fs_info->balance_running))
  3987. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  3988. if (atomic_read(&fs_info->balance_pause_req))
  3989. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  3990. if (atomic_read(&fs_info->balance_cancel_req))
  3991. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  3992. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  3993. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  3994. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  3995. if (lock) {
  3996. spin_lock(&fs_info->balance_lock);
  3997. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3998. spin_unlock(&fs_info->balance_lock);
  3999. } else {
  4000. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  4001. }
  4002. }
  4003. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  4004. {
  4005. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4006. struct btrfs_fs_info *fs_info = root->fs_info;
  4007. struct btrfs_ioctl_balance_args *bargs;
  4008. struct btrfs_balance_control *bctl;
  4009. bool need_unlock; /* for mut. excl. ops lock */
  4010. int ret;
  4011. if (!capable(CAP_SYS_ADMIN))
  4012. return -EPERM;
  4013. ret = mnt_want_write_file(file);
  4014. if (ret)
  4015. return ret;
  4016. again:
  4017. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  4018. mutex_lock(&fs_info->volume_mutex);
  4019. mutex_lock(&fs_info->balance_mutex);
  4020. need_unlock = true;
  4021. goto locked;
  4022. }
  4023. /*
  4024. * mut. excl. ops lock is locked. Three possibilities:
  4025. * (1) some other op is running
  4026. * (2) balance is running
  4027. * (3) balance is paused -- special case (think resume)
  4028. */
  4029. mutex_lock(&fs_info->balance_mutex);
  4030. if (fs_info->balance_ctl) {
  4031. /* this is either (2) or (3) */
  4032. if (!atomic_read(&fs_info->balance_running)) {
  4033. mutex_unlock(&fs_info->balance_mutex);
  4034. if (!mutex_trylock(&fs_info->volume_mutex))
  4035. goto again;
  4036. mutex_lock(&fs_info->balance_mutex);
  4037. if (fs_info->balance_ctl &&
  4038. !atomic_read(&fs_info->balance_running)) {
  4039. /* this is (3) */
  4040. need_unlock = false;
  4041. goto locked;
  4042. }
  4043. mutex_unlock(&fs_info->balance_mutex);
  4044. mutex_unlock(&fs_info->volume_mutex);
  4045. goto again;
  4046. } else {
  4047. /* this is (2) */
  4048. mutex_unlock(&fs_info->balance_mutex);
  4049. ret = -EINPROGRESS;
  4050. goto out;
  4051. }
  4052. } else {
  4053. /* this is (1) */
  4054. mutex_unlock(&fs_info->balance_mutex);
  4055. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  4056. goto out;
  4057. }
  4058. locked:
  4059. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  4060. if (arg) {
  4061. bargs = memdup_user(arg, sizeof(*bargs));
  4062. if (IS_ERR(bargs)) {
  4063. ret = PTR_ERR(bargs);
  4064. goto out_unlock;
  4065. }
  4066. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  4067. if (!fs_info->balance_ctl) {
  4068. ret = -ENOTCONN;
  4069. goto out_bargs;
  4070. }
  4071. bctl = fs_info->balance_ctl;
  4072. spin_lock(&fs_info->balance_lock);
  4073. bctl->flags |= BTRFS_BALANCE_RESUME;
  4074. spin_unlock(&fs_info->balance_lock);
  4075. goto do_balance;
  4076. }
  4077. } else {
  4078. bargs = NULL;
  4079. }
  4080. if (fs_info->balance_ctl) {
  4081. ret = -EINPROGRESS;
  4082. goto out_bargs;
  4083. }
  4084. bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
  4085. if (!bctl) {
  4086. ret = -ENOMEM;
  4087. goto out_bargs;
  4088. }
  4089. bctl->fs_info = fs_info;
  4090. if (arg) {
  4091. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  4092. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  4093. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  4094. bctl->flags = bargs->flags;
  4095. } else {
  4096. /* balance everything - no filters */
  4097. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  4098. }
  4099. if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
  4100. ret = -EINVAL;
  4101. goto out_bctl;
  4102. }
  4103. do_balance:
  4104. /*
  4105. * Ownership of bctl and mutually_exclusive_operation_running
  4106. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  4107. * or, if restriper was paused all the way until unmount, in
  4108. * free_fs_info. mutually_exclusive_operation_running is
  4109. * cleared in __cancel_balance.
  4110. */
  4111. need_unlock = false;
  4112. ret = btrfs_balance(bctl, bargs);
  4113. bctl = NULL;
  4114. if (arg) {
  4115. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  4116. ret = -EFAULT;
  4117. }
  4118. out_bctl:
  4119. kfree(bctl);
  4120. out_bargs:
  4121. kfree(bargs);
  4122. out_unlock:
  4123. mutex_unlock(&fs_info->balance_mutex);
  4124. mutex_unlock(&fs_info->volume_mutex);
  4125. if (need_unlock)
  4126. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  4127. out:
  4128. mnt_drop_write_file(file);
  4129. return ret;
  4130. }
  4131. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  4132. {
  4133. if (!capable(CAP_SYS_ADMIN))
  4134. return -EPERM;
  4135. switch (cmd) {
  4136. case BTRFS_BALANCE_CTL_PAUSE:
  4137. return btrfs_pause_balance(root->fs_info);
  4138. case BTRFS_BALANCE_CTL_CANCEL:
  4139. return btrfs_cancel_balance(root->fs_info);
  4140. }
  4141. return -EINVAL;
  4142. }
  4143. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  4144. void __user *arg)
  4145. {
  4146. struct btrfs_fs_info *fs_info = root->fs_info;
  4147. struct btrfs_ioctl_balance_args *bargs;
  4148. int ret = 0;
  4149. if (!capable(CAP_SYS_ADMIN))
  4150. return -EPERM;
  4151. mutex_lock(&fs_info->balance_mutex);
  4152. if (!fs_info->balance_ctl) {
  4153. ret = -ENOTCONN;
  4154. goto out;
  4155. }
  4156. bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
  4157. if (!bargs) {
  4158. ret = -ENOMEM;
  4159. goto out;
  4160. }
  4161. update_ioctl_balance_args(fs_info, 1, bargs);
  4162. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  4163. ret = -EFAULT;
  4164. kfree(bargs);
  4165. out:
  4166. mutex_unlock(&fs_info->balance_mutex);
  4167. return ret;
  4168. }
  4169. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  4170. {
  4171. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4172. struct btrfs_ioctl_quota_ctl_args *sa;
  4173. struct btrfs_trans_handle *trans = NULL;
  4174. int ret;
  4175. int err;
  4176. if (!capable(CAP_SYS_ADMIN))
  4177. return -EPERM;
  4178. ret = mnt_want_write_file(file);
  4179. if (ret)
  4180. return ret;
  4181. sa = memdup_user(arg, sizeof(*sa));
  4182. if (IS_ERR(sa)) {
  4183. ret = PTR_ERR(sa);
  4184. goto drop_write;
  4185. }
  4186. down_write(&root->fs_info->subvol_sem);
  4187. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  4188. if (IS_ERR(trans)) {
  4189. ret = PTR_ERR(trans);
  4190. goto out;
  4191. }
  4192. switch (sa->cmd) {
  4193. case BTRFS_QUOTA_CTL_ENABLE:
  4194. ret = btrfs_quota_enable(trans, root->fs_info);
  4195. break;
  4196. case BTRFS_QUOTA_CTL_DISABLE:
  4197. ret = btrfs_quota_disable(trans, root->fs_info);
  4198. break;
  4199. default:
  4200. ret = -EINVAL;
  4201. break;
  4202. }
  4203. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  4204. if (err && !ret)
  4205. ret = err;
  4206. out:
  4207. kfree(sa);
  4208. up_write(&root->fs_info->subvol_sem);
  4209. drop_write:
  4210. mnt_drop_write_file(file);
  4211. return ret;
  4212. }
  4213. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  4214. {
  4215. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4216. struct btrfs_ioctl_qgroup_assign_args *sa;
  4217. struct btrfs_trans_handle *trans;
  4218. int ret;
  4219. int err;
  4220. if (!capable(CAP_SYS_ADMIN))
  4221. return -EPERM;
  4222. ret = mnt_want_write_file(file);
  4223. if (ret)
  4224. return ret;
  4225. sa = memdup_user(arg, sizeof(*sa));
  4226. if (IS_ERR(sa)) {
  4227. ret = PTR_ERR(sa);
  4228. goto drop_write;
  4229. }
  4230. trans = btrfs_join_transaction(root);
  4231. if (IS_ERR(trans)) {
  4232. ret = PTR_ERR(trans);
  4233. goto out;
  4234. }
  4235. /* FIXME: check if the IDs really exist */
  4236. if (sa->assign) {
  4237. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  4238. sa->src, sa->dst);
  4239. } else {
  4240. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  4241. sa->src, sa->dst);
  4242. }
  4243. /* update qgroup status and info */
  4244. err = btrfs_run_qgroups(trans, root->fs_info);
  4245. if (err < 0)
  4246. btrfs_handle_fs_error(root->fs_info, err,
  4247. "failed to update qgroup status and info");
  4248. err = btrfs_end_transaction(trans, root);
  4249. if (err && !ret)
  4250. ret = err;
  4251. out:
  4252. kfree(sa);
  4253. drop_write:
  4254. mnt_drop_write_file(file);
  4255. return ret;
  4256. }
  4257. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  4258. {
  4259. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4260. struct btrfs_ioctl_qgroup_create_args *sa;
  4261. struct btrfs_trans_handle *trans;
  4262. int ret;
  4263. int err;
  4264. if (!capable(CAP_SYS_ADMIN))
  4265. return -EPERM;
  4266. ret = mnt_want_write_file(file);
  4267. if (ret)
  4268. return ret;
  4269. sa = memdup_user(arg, sizeof(*sa));
  4270. if (IS_ERR(sa)) {
  4271. ret = PTR_ERR(sa);
  4272. goto drop_write;
  4273. }
  4274. if (!sa->qgroupid) {
  4275. ret = -EINVAL;
  4276. goto out;
  4277. }
  4278. trans = btrfs_join_transaction(root);
  4279. if (IS_ERR(trans)) {
  4280. ret = PTR_ERR(trans);
  4281. goto out;
  4282. }
  4283. /* FIXME: check if the IDs really exist */
  4284. if (sa->create) {
  4285. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
  4286. } else {
  4287. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  4288. }
  4289. err = btrfs_end_transaction(trans, root);
  4290. if (err && !ret)
  4291. ret = err;
  4292. out:
  4293. kfree(sa);
  4294. drop_write:
  4295. mnt_drop_write_file(file);
  4296. return ret;
  4297. }
  4298. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  4299. {
  4300. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4301. struct btrfs_ioctl_qgroup_limit_args *sa;
  4302. struct btrfs_trans_handle *trans;
  4303. int ret;
  4304. int err;
  4305. u64 qgroupid;
  4306. if (!capable(CAP_SYS_ADMIN))
  4307. return -EPERM;
  4308. ret = mnt_want_write_file(file);
  4309. if (ret)
  4310. return ret;
  4311. sa = memdup_user(arg, sizeof(*sa));
  4312. if (IS_ERR(sa)) {
  4313. ret = PTR_ERR(sa);
  4314. goto drop_write;
  4315. }
  4316. trans = btrfs_join_transaction(root);
  4317. if (IS_ERR(trans)) {
  4318. ret = PTR_ERR(trans);
  4319. goto out;
  4320. }
  4321. qgroupid = sa->qgroupid;
  4322. if (!qgroupid) {
  4323. /* take the current subvol as qgroup */
  4324. qgroupid = root->root_key.objectid;
  4325. }
  4326. /* FIXME: check if the IDs really exist */
  4327. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  4328. err = btrfs_end_transaction(trans, root);
  4329. if (err && !ret)
  4330. ret = err;
  4331. out:
  4332. kfree(sa);
  4333. drop_write:
  4334. mnt_drop_write_file(file);
  4335. return ret;
  4336. }
  4337. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  4338. {
  4339. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4340. struct btrfs_ioctl_quota_rescan_args *qsa;
  4341. int ret;
  4342. if (!capable(CAP_SYS_ADMIN))
  4343. return -EPERM;
  4344. ret = mnt_want_write_file(file);
  4345. if (ret)
  4346. return ret;
  4347. qsa = memdup_user(arg, sizeof(*qsa));
  4348. if (IS_ERR(qsa)) {
  4349. ret = PTR_ERR(qsa);
  4350. goto drop_write;
  4351. }
  4352. if (qsa->flags) {
  4353. ret = -EINVAL;
  4354. goto out;
  4355. }
  4356. ret = btrfs_qgroup_rescan(root->fs_info);
  4357. out:
  4358. kfree(qsa);
  4359. drop_write:
  4360. mnt_drop_write_file(file);
  4361. return ret;
  4362. }
  4363. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  4364. {
  4365. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4366. struct btrfs_ioctl_quota_rescan_args *qsa;
  4367. int ret = 0;
  4368. if (!capable(CAP_SYS_ADMIN))
  4369. return -EPERM;
  4370. qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
  4371. if (!qsa)
  4372. return -ENOMEM;
  4373. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  4374. qsa->flags = 1;
  4375. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  4376. }
  4377. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  4378. ret = -EFAULT;
  4379. kfree(qsa);
  4380. return ret;
  4381. }
  4382. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  4383. {
  4384. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4385. if (!capable(CAP_SYS_ADMIN))
  4386. return -EPERM;
  4387. return btrfs_qgroup_wait_for_completion(root->fs_info, true);
  4388. }
  4389. static long _btrfs_ioctl_set_received_subvol(struct file *file,
  4390. struct btrfs_ioctl_received_subvol_args *sa)
  4391. {
  4392. struct inode *inode = file_inode(file);
  4393. struct btrfs_root *root = BTRFS_I(inode)->root;
  4394. struct btrfs_root_item *root_item = &root->root_item;
  4395. struct btrfs_trans_handle *trans;
  4396. struct timespec ct = current_time(inode);
  4397. int ret = 0;
  4398. int received_uuid_changed;
  4399. if (!inode_owner_or_capable(inode))
  4400. return -EPERM;
  4401. ret = mnt_want_write_file(file);
  4402. if (ret < 0)
  4403. return ret;
  4404. down_write(&root->fs_info->subvol_sem);
  4405. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  4406. ret = -EINVAL;
  4407. goto out;
  4408. }
  4409. if (btrfs_root_readonly(root)) {
  4410. ret = -EROFS;
  4411. goto out;
  4412. }
  4413. /*
  4414. * 1 - root item
  4415. * 2 - uuid items (received uuid + subvol uuid)
  4416. */
  4417. trans = btrfs_start_transaction(root, 3);
  4418. if (IS_ERR(trans)) {
  4419. ret = PTR_ERR(trans);
  4420. trans = NULL;
  4421. goto out;
  4422. }
  4423. sa->rtransid = trans->transid;
  4424. sa->rtime.sec = ct.tv_sec;
  4425. sa->rtime.nsec = ct.tv_nsec;
  4426. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  4427. BTRFS_UUID_SIZE);
  4428. if (received_uuid_changed &&
  4429. !btrfs_is_empty_uuid(root_item->received_uuid))
  4430. btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  4431. root_item->received_uuid,
  4432. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4433. root->root_key.objectid);
  4434. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  4435. btrfs_set_root_stransid(root_item, sa->stransid);
  4436. btrfs_set_root_rtransid(root_item, sa->rtransid);
  4437. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  4438. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  4439. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  4440. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  4441. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4442. &root->root_key, &root->root_item);
  4443. if (ret < 0) {
  4444. btrfs_end_transaction(trans, root);
  4445. goto out;
  4446. }
  4447. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  4448. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  4449. sa->uuid,
  4450. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4451. root->root_key.objectid);
  4452. if (ret < 0 && ret != -EEXIST) {
  4453. btrfs_abort_transaction(trans, ret);
  4454. goto out;
  4455. }
  4456. }
  4457. ret = btrfs_commit_transaction(trans, root);
  4458. if (ret < 0) {
  4459. btrfs_abort_transaction(trans, ret);
  4460. goto out;
  4461. }
  4462. out:
  4463. up_write(&root->fs_info->subvol_sem);
  4464. mnt_drop_write_file(file);
  4465. return ret;
  4466. }
  4467. #ifdef CONFIG_64BIT
  4468. static long btrfs_ioctl_set_received_subvol_32(struct file *file,
  4469. void __user *arg)
  4470. {
  4471. struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
  4472. struct btrfs_ioctl_received_subvol_args *args64 = NULL;
  4473. int ret = 0;
  4474. args32 = memdup_user(arg, sizeof(*args32));
  4475. if (IS_ERR(args32)) {
  4476. ret = PTR_ERR(args32);
  4477. args32 = NULL;
  4478. goto out;
  4479. }
  4480. args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
  4481. if (!args64) {
  4482. ret = -ENOMEM;
  4483. goto out;
  4484. }
  4485. memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
  4486. args64->stransid = args32->stransid;
  4487. args64->rtransid = args32->rtransid;
  4488. args64->stime.sec = args32->stime.sec;
  4489. args64->stime.nsec = args32->stime.nsec;
  4490. args64->rtime.sec = args32->rtime.sec;
  4491. args64->rtime.nsec = args32->rtime.nsec;
  4492. args64->flags = args32->flags;
  4493. ret = _btrfs_ioctl_set_received_subvol(file, args64);
  4494. if (ret)
  4495. goto out;
  4496. memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
  4497. args32->stransid = args64->stransid;
  4498. args32->rtransid = args64->rtransid;
  4499. args32->stime.sec = args64->stime.sec;
  4500. args32->stime.nsec = args64->stime.nsec;
  4501. args32->rtime.sec = args64->rtime.sec;
  4502. args32->rtime.nsec = args64->rtime.nsec;
  4503. args32->flags = args64->flags;
  4504. ret = copy_to_user(arg, args32, sizeof(*args32));
  4505. if (ret)
  4506. ret = -EFAULT;
  4507. out:
  4508. kfree(args32);
  4509. kfree(args64);
  4510. return ret;
  4511. }
  4512. #endif
  4513. static long btrfs_ioctl_set_received_subvol(struct file *file,
  4514. void __user *arg)
  4515. {
  4516. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  4517. int ret = 0;
  4518. sa = memdup_user(arg, sizeof(*sa));
  4519. if (IS_ERR(sa)) {
  4520. ret = PTR_ERR(sa);
  4521. sa = NULL;
  4522. goto out;
  4523. }
  4524. ret = _btrfs_ioctl_set_received_subvol(file, sa);
  4525. if (ret)
  4526. goto out;
  4527. ret = copy_to_user(arg, sa, sizeof(*sa));
  4528. if (ret)
  4529. ret = -EFAULT;
  4530. out:
  4531. kfree(sa);
  4532. return ret;
  4533. }
  4534. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  4535. {
  4536. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4537. size_t len;
  4538. int ret;
  4539. char label[BTRFS_LABEL_SIZE];
  4540. spin_lock(&root->fs_info->super_lock);
  4541. memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  4542. spin_unlock(&root->fs_info->super_lock);
  4543. len = strnlen(label, BTRFS_LABEL_SIZE);
  4544. if (len == BTRFS_LABEL_SIZE) {
  4545. btrfs_warn(root->fs_info,
  4546. "label is too long, return the first %zu bytes", --len);
  4547. }
  4548. ret = copy_to_user(arg, label, len);
  4549. return ret ? -EFAULT : 0;
  4550. }
  4551. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  4552. {
  4553. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4554. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4555. struct btrfs_trans_handle *trans;
  4556. char label[BTRFS_LABEL_SIZE];
  4557. int ret;
  4558. if (!capable(CAP_SYS_ADMIN))
  4559. return -EPERM;
  4560. if (copy_from_user(label, arg, sizeof(label)))
  4561. return -EFAULT;
  4562. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  4563. btrfs_err(root->fs_info,
  4564. "unable to set label with more than %d bytes",
  4565. BTRFS_LABEL_SIZE - 1);
  4566. return -EINVAL;
  4567. }
  4568. ret = mnt_want_write_file(file);
  4569. if (ret)
  4570. return ret;
  4571. trans = btrfs_start_transaction(root, 0);
  4572. if (IS_ERR(trans)) {
  4573. ret = PTR_ERR(trans);
  4574. goto out_unlock;
  4575. }
  4576. spin_lock(&root->fs_info->super_lock);
  4577. strcpy(super_block->label, label);
  4578. spin_unlock(&root->fs_info->super_lock);
  4579. ret = btrfs_commit_transaction(trans, root);
  4580. out_unlock:
  4581. mnt_drop_write_file(file);
  4582. return ret;
  4583. }
  4584. #define INIT_FEATURE_FLAGS(suffix) \
  4585. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  4586. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  4587. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  4588. int btrfs_ioctl_get_supported_features(void __user *arg)
  4589. {
  4590. static const struct btrfs_ioctl_feature_flags features[3] = {
  4591. INIT_FEATURE_FLAGS(SUPP),
  4592. INIT_FEATURE_FLAGS(SAFE_SET),
  4593. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  4594. };
  4595. if (copy_to_user(arg, &features, sizeof(features)))
  4596. return -EFAULT;
  4597. return 0;
  4598. }
  4599. static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
  4600. {
  4601. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4602. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4603. struct btrfs_ioctl_feature_flags features;
  4604. features.compat_flags = btrfs_super_compat_flags(super_block);
  4605. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  4606. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  4607. if (copy_to_user(arg, &features, sizeof(features)))
  4608. return -EFAULT;
  4609. return 0;
  4610. }
  4611. static int check_feature_bits(struct btrfs_root *root,
  4612. enum btrfs_feature_set set,
  4613. u64 change_mask, u64 flags, u64 supported_flags,
  4614. u64 safe_set, u64 safe_clear)
  4615. {
  4616. const char *type = btrfs_feature_set_names[set];
  4617. char *names;
  4618. u64 disallowed, unsupported;
  4619. u64 set_mask = flags & change_mask;
  4620. u64 clear_mask = ~flags & change_mask;
  4621. unsupported = set_mask & ~supported_flags;
  4622. if (unsupported) {
  4623. names = btrfs_printable_features(set, unsupported);
  4624. if (names) {
  4625. btrfs_warn(root->fs_info,
  4626. "this kernel does not support the %s feature bit%s",
  4627. names, strchr(names, ',') ? "s" : "");
  4628. kfree(names);
  4629. } else
  4630. btrfs_warn(root->fs_info,
  4631. "this kernel does not support %s bits 0x%llx",
  4632. type, unsupported);
  4633. return -EOPNOTSUPP;
  4634. }
  4635. disallowed = set_mask & ~safe_set;
  4636. if (disallowed) {
  4637. names = btrfs_printable_features(set, disallowed);
  4638. if (names) {
  4639. btrfs_warn(root->fs_info,
  4640. "can't set the %s feature bit%s while mounted",
  4641. names, strchr(names, ',') ? "s" : "");
  4642. kfree(names);
  4643. } else
  4644. btrfs_warn(root->fs_info,
  4645. "can't set %s bits 0x%llx while mounted",
  4646. type, disallowed);
  4647. return -EPERM;
  4648. }
  4649. disallowed = clear_mask & ~safe_clear;
  4650. if (disallowed) {
  4651. names = btrfs_printable_features(set, disallowed);
  4652. if (names) {
  4653. btrfs_warn(root->fs_info,
  4654. "can't clear the %s feature bit%s while mounted",
  4655. names, strchr(names, ',') ? "s" : "");
  4656. kfree(names);
  4657. } else
  4658. btrfs_warn(root->fs_info,
  4659. "can't clear %s bits 0x%llx while mounted",
  4660. type, disallowed);
  4661. return -EPERM;
  4662. }
  4663. return 0;
  4664. }
  4665. #define check_feature(root, change_mask, flags, mask_base) \
  4666. check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
  4667. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  4668. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  4669. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  4670. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  4671. {
  4672. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4673. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4674. struct btrfs_ioctl_feature_flags flags[2];
  4675. struct btrfs_trans_handle *trans;
  4676. u64 newflags;
  4677. int ret;
  4678. if (!capable(CAP_SYS_ADMIN))
  4679. return -EPERM;
  4680. if (copy_from_user(flags, arg, sizeof(flags)))
  4681. return -EFAULT;
  4682. /* Nothing to do */
  4683. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  4684. !flags[0].incompat_flags)
  4685. return 0;
  4686. ret = check_feature(root, flags[0].compat_flags,
  4687. flags[1].compat_flags, COMPAT);
  4688. if (ret)
  4689. return ret;
  4690. ret = check_feature(root, flags[0].compat_ro_flags,
  4691. flags[1].compat_ro_flags, COMPAT_RO);
  4692. if (ret)
  4693. return ret;
  4694. ret = check_feature(root, flags[0].incompat_flags,
  4695. flags[1].incompat_flags, INCOMPAT);
  4696. if (ret)
  4697. return ret;
  4698. ret = mnt_want_write_file(file);
  4699. if (ret)
  4700. return ret;
  4701. trans = btrfs_start_transaction(root, 0);
  4702. if (IS_ERR(trans)) {
  4703. ret = PTR_ERR(trans);
  4704. goto out_drop_write;
  4705. }
  4706. spin_lock(&root->fs_info->super_lock);
  4707. newflags = btrfs_super_compat_flags(super_block);
  4708. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  4709. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  4710. btrfs_set_super_compat_flags(super_block, newflags);
  4711. newflags = btrfs_super_compat_ro_flags(super_block);
  4712. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  4713. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  4714. btrfs_set_super_compat_ro_flags(super_block, newflags);
  4715. newflags = btrfs_super_incompat_flags(super_block);
  4716. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  4717. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  4718. btrfs_set_super_incompat_flags(super_block, newflags);
  4719. spin_unlock(&root->fs_info->super_lock);
  4720. ret = btrfs_commit_transaction(trans, root);
  4721. out_drop_write:
  4722. mnt_drop_write_file(file);
  4723. return ret;
  4724. }
  4725. long btrfs_ioctl(struct file *file, unsigned int
  4726. cmd, unsigned long arg)
  4727. {
  4728. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4729. void __user *argp = (void __user *)arg;
  4730. switch (cmd) {
  4731. case FS_IOC_GETFLAGS:
  4732. return btrfs_ioctl_getflags(file, argp);
  4733. case FS_IOC_SETFLAGS:
  4734. return btrfs_ioctl_setflags(file, argp);
  4735. case FS_IOC_GETVERSION:
  4736. return btrfs_ioctl_getversion(file, argp);
  4737. case FITRIM:
  4738. return btrfs_ioctl_fitrim(file, argp);
  4739. case BTRFS_IOC_SNAP_CREATE:
  4740. return btrfs_ioctl_snap_create(file, argp, 0);
  4741. case BTRFS_IOC_SNAP_CREATE_V2:
  4742. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  4743. case BTRFS_IOC_SUBVOL_CREATE:
  4744. return btrfs_ioctl_snap_create(file, argp, 1);
  4745. case BTRFS_IOC_SUBVOL_CREATE_V2:
  4746. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  4747. case BTRFS_IOC_SNAP_DESTROY:
  4748. return btrfs_ioctl_snap_destroy(file, argp);
  4749. case BTRFS_IOC_SUBVOL_GETFLAGS:
  4750. return btrfs_ioctl_subvol_getflags(file, argp);
  4751. case BTRFS_IOC_SUBVOL_SETFLAGS:
  4752. return btrfs_ioctl_subvol_setflags(file, argp);
  4753. case BTRFS_IOC_DEFAULT_SUBVOL:
  4754. return btrfs_ioctl_default_subvol(file, argp);
  4755. case BTRFS_IOC_DEFRAG:
  4756. return btrfs_ioctl_defrag(file, NULL);
  4757. case BTRFS_IOC_DEFRAG_RANGE:
  4758. return btrfs_ioctl_defrag(file, argp);
  4759. case BTRFS_IOC_RESIZE:
  4760. return btrfs_ioctl_resize(file, argp);
  4761. case BTRFS_IOC_ADD_DEV:
  4762. return btrfs_ioctl_add_dev(root, argp);
  4763. case BTRFS_IOC_RM_DEV:
  4764. return btrfs_ioctl_rm_dev(file, argp);
  4765. case BTRFS_IOC_RM_DEV_V2:
  4766. return btrfs_ioctl_rm_dev_v2(file, argp);
  4767. case BTRFS_IOC_FS_INFO:
  4768. return btrfs_ioctl_fs_info(root, argp);
  4769. case BTRFS_IOC_DEV_INFO:
  4770. return btrfs_ioctl_dev_info(root, argp);
  4771. case BTRFS_IOC_BALANCE:
  4772. return btrfs_ioctl_balance(file, NULL);
  4773. case BTRFS_IOC_TRANS_START:
  4774. return btrfs_ioctl_trans_start(file);
  4775. case BTRFS_IOC_TRANS_END:
  4776. return btrfs_ioctl_trans_end(file);
  4777. case BTRFS_IOC_TREE_SEARCH:
  4778. return btrfs_ioctl_tree_search(file, argp);
  4779. case BTRFS_IOC_TREE_SEARCH_V2:
  4780. return btrfs_ioctl_tree_search_v2(file, argp);
  4781. case BTRFS_IOC_INO_LOOKUP:
  4782. return btrfs_ioctl_ino_lookup(file, argp);
  4783. case BTRFS_IOC_INO_PATHS:
  4784. return btrfs_ioctl_ino_to_path(root, argp);
  4785. case BTRFS_IOC_LOGICAL_INO:
  4786. return btrfs_ioctl_logical_to_ino(root, argp);
  4787. case BTRFS_IOC_SPACE_INFO:
  4788. return btrfs_ioctl_space_info(root, argp);
  4789. case BTRFS_IOC_SYNC: {
  4790. int ret;
  4791. ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
  4792. if (ret)
  4793. return ret;
  4794. ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
  4795. /*
  4796. * The transaction thread may want to do more work,
  4797. * namely it pokes the cleaner kthread that will start
  4798. * processing uncleaned subvols.
  4799. */
  4800. wake_up_process(root->fs_info->transaction_kthread);
  4801. return ret;
  4802. }
  4803. case BTRFS_IOC_START_SYNC:
  4804. return btrfs_ioctl_start_sync(root, argp);
  4805. case BTRFS_IOC_WAIT_SYNC:
  4806. return btrfs_ioctl_wait_sync(root, argp);
  4807. case BTRFS_IOC_SCRUB:
  4808. return btrfs_ioctl_scrub(file, argp);
  4809. case BTRFS_IOC_SCRUB_CANCEL:
  4810. return btrfs_ioctl_scrub_cancel(root, argp);
  4811. case BTRFS_IOC_SCRUB_PROGRESS:
  4812. return btrfs_ioctl_scrub_progress(root, argp);
  4813. case BTRFS_IOC_BALANCE_V2:
  4814. return btrfs_ioctl_balance(file, argp);
  4815. case BTRFS_IOC_BALANCE_CTL:
  4816. return btrfs_ioctl_balance_ctl(root, arg);
  4817. case BTRFS_IOC_BALANCE_PROGRESS:
  4818. return btrfs_ioctl_balance_progress(root, argp);
  4819. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  4820. return btrfs_ioctl_set_received_subvol(file, argp);
  4821. #ifdef CONFIG_64BIT
  4822. case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
  4823. return btrfs_ioctl_set_received_subvol_32(file, argp);
  4824. #endif
  4825. case BTRFS_IOC_SEND:
  4826. return btrfs_ioctl_send(file, argp);
  4827. case BTRFS_IOC_GET_DEV_STATS:
  4828. return btrfs_ioctl_get_dev_stats(root, argp);
  4829. case BTRFS_IOC_QUOTA_CTL:
  4830. return btrfs_ioctl_quota_ctl(file, argp);
  4831. case BTRFS_IOC_QGROUP_ASSIGN:
  4832. return btrfs_ioctl_qgroup_assign(file, argp);
  4833. case BTRFS_IOC_QGROUP_CREATE:
  4834. return btrfs_ioctl_qgroup_create(file, argp);
  4835. case BTRFS_IOC_QGROUP_LIMIT:
  4836. return btrfs_ioctl_qgroup_limit(file, argp);
  4837. case BTRFS_IOC_QUOTA_RESCAN:
  4838. return btrfs_ioctl_quota_rescan(file, argp);
  4839. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  4840. return btrfs_ioctl_quota_rescan_status(file, argp);
  4841. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  4842. return btrfs_ioctl_quota_rescan_wait(file, argp);
  4843. case BTRFS_IOC_DEV_REPLACE:
  4844. return btrfs_ioctl_dev_replace(root, argp);
  4845. case BTRFS_IOC_GET_FSLABEL:
  4846. return btrfs_ioctl_get_fslabel(file, argp);
  4847. case BTRFS_IOC_SET_FSLABEL:
  4848. return btrfs_ioctl_set_fslabel(file, argp);
  4849. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  4850. return btrfs_ioctl_get_supported_features(argp);
  4851. case BTRFS_IOC_GET_FEATURES:
  4852. return btrfs_ioctl_get_features(file, argp);
  4853. case BTRFS_IOC_SET_FEATURES:
  4854. return btrfs_ioctl_set_features(file, argp);
  4855. }
  4856. return -ENOTTY;
  4857. }
  4858. #ifdef CONFIG_COMPAT
  4859. long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4860. {
  4861. /*
  4862. * These all access 32-bit values anyway so no further
  4863. * handling is necessary.
  4864. */
  4865. switch (cmd) {
  4866. case FS_IOC32_GETFLAGS:
  4867. cmd = FS_IOC_GETFLAGS;
  4868. break;
  4869. case FS_IOC32_SETFLAGS:
  4870. cmd = FS_IOC_SETFLAGS;
  4871. break;
  4872. case FS_IOC32_GETVERSION:
  4873. cmd = FS_IOC_GETVERSION;
  4874. break;
  4875. }
  4876. return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  4877. }
  4878. #endif