ioctl.c 137 KB

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