ioctl.c 128 KB

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