ioctl.c 137 KB

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