ioctl.c 106 KB

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