ioctl.c 135 KB

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