ioctl.c 135 KB

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