ioctl.c 114 KB

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