ioctl.c 129 KB

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