ioctl.c 135 KB

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