ioctl.c 137 KB

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