ioctl.c 146 KB

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