ioctl.c 146 KB

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