ioctl.c 133 KB

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