ioctl.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542
  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_dir_item *di;
  1499. struct btrfs_key key;
  1500. u64 dir_id;
  1501. int ret;
  1502. path = btrfs_alloc_path();
  1503. if (!path)
  1504. return -ENOMEM;
  1505. /* Make sure this root isn't set as the default subvol */
  1506. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  1507. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
  1508. dir_id, "default", 7, 0);
  1509. if (di && !IS_ERR(di)) {
  1510. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1511. if (key.objectid == root->root_key.objectid) {
  1512. ret = -ENOTEMPTY;
  1513. goto out;
  1514. }
  1515. btrfs_release_path(path);
  1516. }
  1517. key.objectid = root->root_key.objectid;
  1518. key.type = BTRFS_ROOT_REF_KEY;
  1519. key.offset = (u64)-1;
  1520. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1521. &key, path, 0, 0);
  1522. if (ret < 0)
  1523. goto out;
  1524. BUG_ON(ret == 0);
  1525. ret = 0;
  1526. if (path->slots[0] > 0) {
  1527. path->slots[0]--;
  1528. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1529. if (key.objectid == root->root_key.objectid &&
  1530. key.type == BTRFS_ROOT_REF_KEY)
  1531. ret = -ENOTEMPTY;
  1532. }
  1533. out:
  1534. btrfs_free_path(path);
  1535. return ret;
  1536. }
  1537. static noinline int key_in_sk(struct btrfs_key *key,
  1538. struct btrfs_ioctl_search_key *sk)
  1539. {
  1540. struct btrfs_key test;
  1541. int ret;
  1542. test.objectid = sk->min_objectid;
  1543. test.type = sk->min_type;
  1544. test.offset = sk->min_offset;
  1545. ret = btrfs_comp_cpu_keys(key, &test);
  1546. if (ret < 0)
  1547. return 0;
  1548. test.objectid = sk->max_objectid;
  1549. test.type = sk->max_type;
  1550. test.offset = sk->max_offset;
  1551. ret = btrfs_comp_cpu_keys(key, &test);
  1552. if (ret > 0)
  1553. return 0;
  1554. return 1;
  1555. }
  1556. static noinline int copy_to_sk(struct btrfs_root *root,
  1557. struct btrfs_path *path,
  1558. struct btrfs_key *key,
  1559. struct btrfs_ioctl_search_key *sk,
  1560. char *buf,
  1561. unsigned long *sk_offset,
  1562. int *num_found)
  1563. {
  1564. u64 found_transid;
  1565. struct extent_buffer *leaf;
  1566. struct btrfs_ioctl_search_header sh;
  1567. unsigned long item_off;
  1568. unsigned long item_len;
  1569. int nritems;
  1570. int i;
  1571. int slot;
  1572. int ret = 0;
  1573. leaf = path->nodes[0];
  1574. slot = path->slots[0];
  1575. nritems = btrfs_header_nritems(leaf);
  1576. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1577. i = nritems;
  1578. goto advance_key;
  1579. }
  1580. found_transid = btrfs_header_generation(leaf);
  1581. for (i = slot; i < nritems; i++) {
  1582. item_off = btrfs_item_ptr_offset(leaf, i);
  1583. item_len = btrfs_item_size_nr(leaf, i);
  1584. btrfs_item_key_to_cpu(leaf, key, i);
  1585. if (!key_in_sk(key, sk))
  1586. continue;
  1587. if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1588. item_len = 0;
  1589. if (sizeof(sh) + item_len + *sk_offset >
  1590. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1591. ret = 1;
  1592. goto overflow;
  1593. }
  1594. sh.objectid = key->objectid;
  1595. sh.offset = key->offset;
  1596. sh.type = key->type;
  1597. sh.len = item_len;
  1598. sh.transid = found_transid;
  1599. /* copy search result header */
  1600. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1601. *sk_offset += sizeof(sh);
  1602. if (item_len) {
  1603. char *p = buf + *sk_offset;
  1604. /* copy the item */
  1605. read_extent_buffer(leaf, p,
  1606. item_off, item_len);
  1607. *sk_offset += item_len;
  1608. }
  1609. (*num_found)++;
  1610. if (*num_found >= sk->nr_items)
  1611. break;
  1612. }
  1613. advance_key:
  1614. ret = 0;
  1615. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1616. key->offset++;
  1617. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1618. key->offset = 0;
  1619. key->type++;
  1620. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1621. key->offset = 0;
  1622. key->type = 0;
  1623. key->objectid++;
  1624. } else
  1625. ret = 1;
  1626. overflow:
  1627. return ret;
  1628. }
  1629. static noinline int search_ioctl(struct inode *inode,
  1630. struct btrfs_ioctl_search_args *args)
  1631. {
  1632. struct btrfs_root *root;
  1633. struct btrfs_key key;
  1634. struct btrfs_key max_key;
  1635. struct btrfs_path *path;
  1636. struct btrfs_ioctl_search_key *sk = &args->key;
  1637. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1638. int ret;
  1639. int num_found = 0;
  1640. unsigned long sk_offset = 0;
  1641. path = btrfs_alloc_path();
  1642. if (!path)
  1643. return -ENOMEM;
  1644. if (sk->tree_id == 0) {
  1645. /* search the root of the inode that was passed */
  1646. root = BTRFS_I(inode)->root;
  1647. } else {
  1648. key.objectid = sk->tree_id;
  1649. key.type = BTRFS_ROOT_ITEM_KEY;
  1650. key.offset = (u64)-1;
  1651. root = btrfs_read_fs_root_no_name(info, &key);
  1652. if (IS_ERR(root)) {
  1653. printk(KERN_ERR "could not find root %llu\n",
  1654. sk->tree_id);
  1655. btrfs_free_path(path);
  1656. return -ENOENT;
  1657. }
  1658. }
  1659. key.objectid = sk->min_objectid;
  1660. key.type = sk->min_type;
  1661. key.offset = sk->min_offset;
  1662. max_key.objectid = sk->max_objectid;
  1663. max_key.type = sk->max_type;
  1664. max_key.offset = sk->max_offset;
  1665. path->keep_locks = 1;
  1666. while(1) {
  1667. ret = btrfs_search_forward(root, &key, &max_key, path,
  1668. sk->min_transid);
  1669. if (ret != 0) {
  1670. if (ret > 0)
  1671. ret = 0;
  1672. goto err;
  1673. }
  1674. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1675. &sk_offset, &num_found);
  1676. btrfs_release_path(path);
  1677. if (ret || num_found >= sk->nr_items)
  1678. break;
  1679. }
  1680. ret = 0;
  1681. err:
  1682. sk->nr_items = num_found;
  1683. btrfs_free_path(path);
  1684. return ret;
  1685. }
  1686. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1687. void __user *argp)
  1688. {
  1689. struct btrfs_ioctl_search_args *args;
  1690. struct inode *inode;
  1691. int ret;
  1692. if (!capable(CAP_SYS_ADMIN))
  1693. return -EPERM;
  1694. args = memdup_user(argp, sizeof(*args));
  1695. if (IS_ERR(args))
  1696. return PTR_ERR(args);
  1697. inode = file_inode(file);
  1698. ret = search_ioctl(inode, args);
  1699. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1700. ret = -EFAULT;
  1701. kfree(args);
  1702. return ret;
  1703. }
  1704. /*
  1705. * Search INODE_REFs to identify path name of 'dirid' directory
  1706. * in a 'tree_id' tree. and sets path name to 'name'.
  1707. */
  1708. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1709. u64 tree_id, u64 dirid, char *name)
  1710. {
  1711. struct btrfs_root *root;
  1712. struct btrfs_key key;
  1713. char *ptr;
  1714. int ret = -1;
  1715. int slot;
  1716. int len;
  1717. int total_len = 0;
  1718. struct btrfs_inode_ref *iref;
  1719. struct extent_buffer *l;
  1720. struct btrfs_path *path;
  1721. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1722. name[0]='\0';
  1723. return 0;
  1724. }
  1725. path = btrfs_alloc_path();
  1726. if (!path)
  1727. return -ENOMEM;
  1728. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1729. key.objectid = tree_id;
  1730. key.type = BTRFS_ROOT_ITEM_KEY;
  1731. key.offset = (u64)-1;
  1732. root = btrfs_read_fs_root_no_name(info, &key);
  1733. if (IS_ERR(root)) {
  1734. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1735. ret = -ENOENT;
  1736. goto out;
  1737. }
  1738. key.objectid = dirid;
  1739. key.type = BTRFS_INODE_REF_KEY;
  1740. key.offset = (u64)-1;
  1741. while(1) {
  1742. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1743. if (ret < 0)
  1744. goto out;
  1745. l = path->nodes[0];
  1746. slot = path->slots[0];
  1747. if (ret > 0 && slot > 0)
  1748. slot--;
  1749. btrfs_item_key_to_cpu(l, &key, slot);
  1750. if (ret > 0 && (key.objectid != dirid ||
  1751. key.type != BTRFS_INODE_REF_KEY)) {
  1752. ret = -ENOENT;
  1753. goto out;
  1754. }
  1755. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1756. len = btrfs_inode_ref_name_len(l, iref);
  1757. ptr -= len + 1;
  1758. total_len += len + 1;
  1759. if (ptr < name) {
  1760. ret = -ENAMETOOLONG;
  1761. goto out;
  1762. }
  1763. *(ptr + len) = '/';
  1764. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1765. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1766. break;
  1767. btrfs_release_path(path);
  1768. key.objectid = key.offset;
  1769. key.offset = (u64)-1;
  1770. dirid = key.objectid;
  1771. }
  1772. memmove(name, ptr, total_len);
  1773. name[total_len]='\0';
  1774. ret = 0;
  1775. out:
  1776. btrfs_free_path(path);
  1777. return ret;
  1778. }
  1779. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1780. void __user *argp)
  1781. {
  1782. struct btrfs_ioctl_ino_lookup_args *args;
  1783. struct inode *inode;
  1784. int ret;
  1785. if (!capable(CAP_SYS_ADMIN))
  1786. return -EPERM;
  1787. args = memdup_user(argp, sizeof(*args));
  1788. if (IS_ERR(args))
  1789. return PTR_ERR(args);
  1790. inode = file_inode(file);
  1791. if (args->treeid == 0)
  1792. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1793. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1794. args->treeid, args->objectid,
  1795. args->name);
  1796. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1797. ret = -EFAULT;
  1798. kfree(args);
  1799. return ret;
  1800. }
  1801. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1802. void __user *arg)
  1803. {
  1804. struct dentry *parent = fdentry(file);
  1805. struct dentry *dentry;
  1806. struct inode *dir = parent->d_inode;
  1807. struct inode *inode;
  1808. struct btrfs_root *root = BTRFS_I(dir)->root;
  1809. struct btrfs_root *dest = NULL;
  1810. struct btrfs_ioctl_vol_args *vol_args;
  1811. struct btrfs_trans_handle *trans;
  1812. struct btrfs_block_rsv block_rsv;
  1813. u64 qgroup_reserved;
  1814. int namelen;
  1815. int ret;
  1816. int err = 0;
  1817. vol_args = memdup_user(arg, sizeof(*vol_args));
  1818. if (IS_ERR(vol_args))
  1819. return PTR_ERR(vol_args);
  1820. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1821. namelen = strlen(vol_args->name);
  1822. if (strchr(vol_args->name, '/') ||
  1823. strncmp(vol_args->name, "..", namelen) == 0) {
  1824. err = -EINVAL;
  1825. goto out;
  1826. }
  1827. err = mnt_want_write_file(file);
  1828. if (err)
  1829. goto out;
  1830. err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1831. if (err == -EINTR)
  1832. goto out;
  1833. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1834. if (IS_ERR(dentry)) {
  1835. err = PTR_ERR(dentry);
  1836. goto out_unlock_dir;
  1837. }
  1838. if (!dentry->d_inode) {
  1839. err = -ENOENT;
  1840. goto out_dput;
  1841. }
  1842. inode = dentry->d_inode;
  1843. dest = BTRFS_I(inode)->root;
  1844. if (!capable(CAP_SYS_ADMIN)){
  1845. /*
  1846. * Regular user. Only allow this with a special mount
  1847. * option, when the user has write+exec access to the
  1848. * subvol root, and when rmdir(2) would have been
  1849. * allowed.
  1850. *
  1851. * Note that this is _not_ check that the subvol is
  1852. * empty or doesn't contain data that we wouldn't
  1853. * otherwise be able to delete.
  1854. *
  1855. * Users who want to delete empty subvols should try
  1856. * rmdir(2).
  1857. */
  1858. err = -EPERM;
  1859. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1860. goto out_dput;
  1861. /*
  1862. * Do not allow deletion if the parent dir is the same
  1863. * as the dir to be deleted. That means the ioctl
  1864. * must be called on the dentry referencing the root
  1865. * of the subvol, not a random directory contained
  1866. * within it.
  1867. */
  1868. err = -EINVAL;
  1869. if (root == dest)
  1870. goto out_dput;
  1871. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1872. if (err)
  1873. goto out_dput;
  1874. }
  1875. /* check if subvolume may be deleted by a user */
  1876. err = btrfs_may_delete(dir, dentry, 1);
  1877. if (err)
  1878. goto out_dput;
  1879. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1880. err = -EINVAL;
  1881. goto out_dput;
  1882. }
  1883. mutex_lock(&inode->i_mutex);
  1884. err = d_invalidate(dentry);
  1885. if (err)
  1886. goto out_unlock;
  1887. down_write(&root->fs_info->subvol_sem);
  1888. err = may_destroy_subvol(dest);
  1889. if (err)
  1890. goto out_up_write;
  1891. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  1892. /*
  1893. * One for dir inode, two for dir entries, two for root
  1894. * ref/backref.
  1895. */
  1896. err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  1897. 5, &qgroup_reserved, true);
  1898. if (err)
  1899. goto out_up_write;
  1900. trans = btrfs_start_transaction(root, 0);
  1901. if (IS_ERR(trans)) {
  1902. err = PTR_ERR(trans);
  1903. goto out_release;
  1904. }
  1905. trans->block_rsv = &block_rsv;
  1906. trans->bytes_reserved = block_rsv.size;
  1907. ret = btrfs_unlink_subvol(trans, root, dir,
  1908. dest->root_key.objectid,
  1909. dentry->d_name.name,
  1910. dentry->d_name.len);
  1911. if (ret) {
  1912. err = ret;
  1913. btrfs_abort_transaction(trans, root, ret);
  1914. goto out_end_trans;
  1915. }
  1916. btrfs_record_root_in_trans(trans, dest);
  1917. memset(&dest->root_item.drop_progress, 0,
  1918. sizeof(dest->root_item.drop_progress));
  1919. dest->root_item.drop_level = 0;
  1920. btrfs_set_root_refs(&dest->root_item, 0);
  1921. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1922. ret = btrfs_insert_orphan_item(trans,
  1923. root->fs_info->tree_root,
  1924. dest->root_key.objectid);
  1925. if (ret) {
  1926. btrfs_abort_transaction(trans, root, ret);
  1927. err = ret;
  1928. goto out_end_trans;
  1929. }
  1930. }
  1931. out_end_trans:
  1932. trans->block_rsv = NULL;
  1933. trans->bytes_reserved = 0;
  1934. ret = btrfs_end_transaction(trans, root);
  1935. if (ret && !err)
  1936. err = ret;
  1937. inode->i_flags |= S_DEAD;
  1938. out_release:
  1939. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  1940. out_up_write:
  1941. up_write(&root->fs_info->subvol_sem);
  1942. out_unlock:
  1943. mutex_unlock(&inode->i_mutex);
  1944. if (!err) {
  1945. shrink_dcache_sb(root->fs_info->sb);
  1946. btrfs_invalidate_inodes(dest);
  1947. d_delete(dentry);
  1948. /* the last ref */
  1949. if (dest->cache_inode) {
  1950. iput(dest->cache_inode);
  1951. dest->cache_inode = NULL;
  1952. }
  1953. }
  1954. out_dput:
  1955. dput(dentry);
  1956. out_unlock_dir:
  1957. mutex_unlock(&dir->i_mutex);
  1958. mnt_drop_write_file(file);
  1959. out:
  1960. kfree(vol_args);
  1961. return err;
  1962. }
  1963. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1964. {
  1965. struct inode *inode = file_inode(file);
  1966. struct btrfs_root *root = BTRFS_I(inode)->root;
  1967. struct btrfs_ioctl_defrag_range_args *range;
  1968. int ret;
  1969. ret = mnt_want_write_file(file);
  1970. if (ret)
  1971. return ret;
  1972. if (btrfs_root_readonly(root)) {
  1973. ret = -EROFS;
  1974. goto out;
  1975. }
  1976. switch (inode->i_mode & S_IFMT) {
  1977. case S_IFDIR:
  1978. if (!capable(CAP_SYS_ADMIN)) {
  1979. ret = -EPERM;
  1980. goto out;
  1981. }
  1982. ret = btrfs_defrag_root(root);
  1983. if (ret)
  1984. goto out;
  1985. ret = btrfs_defrag_root(root->fs_info->extent_root);
  1986. break;
  1987. case S_IFREG:
  1988. if (!(file->f_mode & FMODE_WRITE)) {
  1989. ret = -EINVAL;
  1990. goto out;
  1991. }
  1992. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1993. if (!range) {
  1994. ret = -ENOMEM;
  1995. goto out;
  1996. }
  1997. if (argp) {
  1998. if (copy_from_user(range, argp,
  1999. sizeof(*range))) {
  2000. ret = -EFAULT;
  2001. kfree(range);
  2002. goto out;
  2003. }
  2004. /* compression requires us to start the IO */
  2005. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2006. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2007. range->extent_thresh = (u32)-1;
  2008. }
  2009. } else {
  2010. /* the rest are all set to zero by kzalloc */
  2011. range->len = (u64)-1;
  2012. }
  2013. ret = btrfs_defrag_file(file_inode(file), file,
  2014. range, 0, 0);
  2015. if (ret > 0)
  2016. ret = 0;
  2017. kfree(range);
  2018. break;
  2019. default:
  2020. ret = -EINVAL;
  2021. }
  2022. out:
  2023. mnt_drop_write_file(file);
  2024. return ret;
  2025. }
  2026. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  2027. {
  2028. struct btrfs_ioctl_vol_args *vol_args;
  2029. int ret;
  2030. if (!capable(CAP_SYS_ADMIN))
  2031. return -EPERM;
  2032. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2033. 1)) {
  2034. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2035. return -EINVAL;
  2036. }
  2037. mutex_lock(&root->fs_info->volume_mutex);
  2038. vol_args = memdup_user(arg, sizeof(*vol_args));
  2039. if (IS_ERR(vol_args)) {
  2040. ret = PTR_ERR(vol_args);
  2041. goto out;
  2042. }
  2043. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2044. ret = btrfs_init_new_device(root, vol_args->name);
  2045. kfree(vol_args);
  2046. out:
  2047. mutex_unlock(&root->fs_info->volume_mutex);
  2048. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2049. return ret;
  2050. }
  2051. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2052. {
  2053. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2054. struct btrfs_ioctl_vol_args *vol_args;
  2055. int ret;
  2056. if (!capable(CAP_SYS_ADMIN))
  2057. return -EPERM;
  2058. ret = mnt_want_write_file(file);
  2059. if (ret)
  2060. return ret;
  2061. vol_args = memdup_user(arg, sizeof(*vol_args));
  2062. if (IS_ERR(vol_args)) {
  2063. ret = PTR_ERR(vol_args);
  2064. goto out;
  2065. }
  2066. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2067. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2068. 1)) {
  2069. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2070. goto out;
  2071. }
  2072. mutex_lock(&root->fs_info->volume_mutex);
  2073. ret = btrfs_rm_device(root, vol_args->name);
  2074. mutex_unlock(&root->fs_info->volume_mutex);
  2075. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2076. out:
  2077. kfree(vol_args);
  2078. mnt_drop_write_file(file);
  2079. return ret;
  2080. }
  2081. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2082. {
  2083. struct btrfs_ioctl_fs_info_args *fi_args;
  2084. struct btrfs_device *device;
  2085. struct btrfs_device *next;
  2086. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2087. int ret = 0;
  2088. if (!capable(CAP_SYS_ADMIN))
  2089. return -EPERM;
  2090. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2091. if (!fi_args)
  2092. return -ENOMEM;
  2093. fi_args->num_devices = fs_devices->num_devices;
  2094. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2095. mutex_lock(&fs_devices->device_list_mutex);
  2096. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2097. if (device->devid > fi_args->max_id)
  2098. fi_args->max_id = device->devid;
  2099. }
  2100. mutex_unlock(&fs_devices->device_list_mutex);
  2101. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2102. ret = -EFAULT;
  2103. kfree(fi_args);
  2104. return ret;
  2105. }
  2106. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2107. {
  2108. struct btrfs_ioctl_dev_info_args *di_args;
  2109. struct btrfs_device *dev;
  2110. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2111. int ret = 0;
  2112. char *s_uuid = NULL;
  2113. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  2114. if (!capable(CAP_SYS_ADMIN))
  2115. return -EPERM;
  2116. di_args = memdup_user(arg, sizeof(*di_args));
  2117. if (IS_ERR(di_args))
  2118. return PTR_ERR(di_args);
  2119. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  2120. s_uuid = di_args->uuid;
  2121. mutex_lock(&fs_devices->device_list_mutex);
  2122. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2123. if (!dev) {
  2124. ret = -ENODEV;
  2125. goto out;
  2126. }
  2127. di_args->devid = dev->devid;
  2128. di_args->bytes_used = dev->bytes_used;
  2129. di_args->total_bytes = dev->total_bytes;
  2130. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2131. if (dev->name) {
  2132. struct rcu_string *name;
  2133. rcu_read_lock();
  2134. name = rcu_dereference(dev->name);
  2135. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2136. rcu_read_unlock();
  2137. di_args->path[sizeof(di_args->path) - 1] = 0;
  2138. } else {
  2139. di_args->path[0] = '\0';
  2140. }
  2141. out:
  2142. mutex_unlock(&fs_devices->device_list_mutex);
  2143. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2144. ret = -EFAULT;
  2145. kfree(di_args);
  2146. return ret;
  2147. }
  2148. static struct page *extent_same_get_page(struct inode *inode, u64 off)
  2149. {
  2150. struct page *page;
  2151. pgoff_t index;
  2152. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2153. index = off >> PAGE_CACHE_SHIFT;
  2154. page = grab_cache_page(inode->i_mapping, index);
  2155. if (!page)
  2156. return NULL;
  2157. if (!PageUptodate(page)) {
  2158. if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
  2159. 0))
  2160. return NULL;
  2161. lock_page(page);
  2162. if (!PageUptodate(page)) {
  2163. unlock_page(page);
  2164. page_cache_release(page);
  2165. return NULL;
  2166. }
  2167. }
  2168. unlock_page(page);
  2169. return page;
  2170. }
  2171. static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
  2172. {
  2173. /* do any pending delalloc/csum calc on src, one way or
  2174. another, and lock file content */
  2175. while (1) {
  2176. struct btrfs_ordered_extent *ordered;
  2177. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2178. ordered = btrfs_lookup_first_ordered_extent(inode,
  2179. off + len - 1);
  2180. if (!ordered &&
  2181. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2182. off + len - 1, EXTENT_DELALLOC, 0, NULL))
  2183. break;
  2184. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2185. if (ordered)
  2186. btrfs_put_ordered_extent(ordered);
  2187. btrfs_wait_ordered_range(inode, off, len);
  2188. }
  2189. }
  2190. static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
  2191. struct inode *inode2, u64 loff2, u64 len)
  2192. {
  2193. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2194. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2195. mutex_unlock(&inode1->i_mutex);
  2196. mutex_unlock(&inode2->i_mutex);
  2197. }
  2198. static void btrfs_double_lock(struct inode *inode1, u64 loff1,
  2199. struct inode *inode2, u64 loff2, u64 len)
  2200. {
  2201. if (inode1 < inode2) {
  2202. swap(inode1, inode2);
  2203. swap(loff1, loff2);
  2204. }
  2205. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  2206. lock_extent_range(inode1, loff1, len);
  2207. if (inode1 != inode2) {
  2208. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  2209. lock_extent_range(inode2, loff2, len);
  2210. }
  2211. }
  2212. static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
  2213. u64 dst_loff, u64 len)
  2214. {
  2215. int ret = 0;
  2216. struct page *src_page, *dst_page;
  2217. unsigned int cmp_len = PAGE_CACHE_SIZE;
  2218. void *addr, *dst_addr;
  2219. while (len) {
  2220. if (len < PAGE_CACHE_SIZE)
  2221. cmp_len = len;
  2222. src_page = extent_same_get_page(src, loff);
  2223. if (!src_page)
  2224. return -EINVAL;
  2225. dst_page = extent_same_get_page(dst, dst_loff);
  2226. if (!dst_page) {
  2227. page_cache_release(src_page);
  2228. return -EINVAL;
  2229. }
  2230. addr = kmap_atomic(src_page);
  2231. dst_addr = kmap_atomic(dst_page);
  2232. flush_dcache_page(src_page);
  2233. flush_dcache_page(dst_page);
  2234. if (memcmp(addr, dst_addr, cmp_len))
  2235. ret = BTRFS_SAME_DATA_DIFFERS;
  2236. kunmap_atomic(addr);
  2237. kunmap_atomic(dst_addr);
  2238. page_cache_release(src_page);
  2239. page_cache_release(dst_page);
  2240. if (ret)
  2241. break;
  2242. loff += cmp_len;
  2243. dst_loff += cmp_len;
  2244. len -= cmp_len;
  2245. }
  2246. return ret;
  2247. }
  2248. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
  2249. {
  2250. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2251. if (off + len > inode->i_size || off + len < off)
  2252. return -EINVAL;
  2253. /* Check that we are block aligned - btrfs_clone() requires this */
  2254. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2255. return -EINVAL;
  2256. return 0;
  2257. }
  2258. static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
  2259. struct inode *dst, u64 dst_loff)
  2260. {
  2261. int ret;
  2262. /*
  2263. * btrfs_clone() can't handle extents in the same file
  2264. * yet. Once that works, we can drop this check and replace it
  2265. * with a check for the same inode, but overlapping extents.
  2266. */
  2267. if (src == dst)
  2268. return -EINVAL;
  2269. btrfs_double_lock(src, loff, dst, dst_loff, len);
  2270. ret = extent_same_check_offsets(src, loff, len);
  2271. if (ret)
  2272. goto out_unlock;
  2273. ret = extent_same_check_offsets(dst, dst_loff, len);
  2274. if (ret)
  2275. goto out_unlock;
  2276. /* don't make the dst file partly checksummed */
  2277. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2278. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  2279. ret = -EINVAL;
  2280. goto out_unlock;
  2281. }
  2282. ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
  2283. if (ret == 0)
  2284. ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
  2285. out_unlock:
  2286. btrfs_double_unlock(src, loff, dst, dst_loff, len);
  2287. return ret;
  2288. }
  2289. #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
  2290. static long btrfs_ioctl_file_extent_same(struct file *file,
  2291. void __user *argp)
  2292. {
  2293. struct btrfs_ioctl_same_args *args = argp;
  2294. struct btrfs_ioctl_same_args same;
  2295. struct btrfs_ioctl_same_extent_info info;
  2296. struct inode *src = file->f_dentry->d_inode;
  2297. struct file *dst_file = NULL;
  2298. struct inode *dst;
  2299. u64 off;
  2300. u64 len;
  2301. int i;
  2302. int ret;
  2303. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  2304. bool is_admin = capable(CAP_SYS_ADMIN);
  2305. if (!(file->f_mode & FMODE_READ))
  2306. return -EINVAL;
  2307. ret = mnt_want_write_file(file);
  2308. if (ret)
  2309. return ret;
  2310. if (copy_from_user(&same,
  2311. (struct btrfs_ioctl_same_args __user *)argp,
  2312. sizeof(same))) {
  2313. ret = -EFAULT;
  2314. goto out;
  2315. }
  2316. off = same.logical_offset;
  2317. len = same.length;
  2318. /*
  2319. * Limit the total length we will dedupe for each operation.
  2320. * This is intended to bound the total time spent in this
  2321. * ioctl to something sane.
  2322. */
  2323. if (len > BTRFS_MAX_DEDUPE_LEN)
  2324. len = BTRFS_MAX_DEDUPE_LEN;
  2325. if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
  2326. /*
  2327. * Btrfs does not support blocksize < page_size. As a
  2328. * result, btrfs_cmp_data() won't correctly handle
  2329. * this situation without an update.
  2330. */
  2331. ret = -EINVAL;
  2332. goto out;
  2333. }
  2334. ret = -EISDIR;
  2335. if (S_ISDIR(src->i_mode))
  2336. goto out;
  2337. ret = -EACCES;
  2338. if (!S_ISREG(src->i_mode))
  2339. goto out;
  2340. ret = 0;
  2341. for (i = 0; i < same.dest_count; i++) {
  2342. if (copy_from_user(&info, &args->info[i], sizeof(info))) {
  2343. ret = -EFAULT;
  2344. goto out;
  2345. }
  2346. info.bytes_deduped = 0;
  2347. dst_file = fget(info.fd);
  2348. if (!dst_file) {
  2349. info.status = -EBADF;
  2350. goto next;
  2351. }
  2352. if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
  2353. info.status = -EINVAL;
  2354. goto next;
  2355. }
  2356. info.status = -EXDEV;
  2357. if (file->f_path.mnt != dst_file->f_path.mnt)
  2358. goto next;
  2359. dst = dst_file->f_dentry->d_inode;
  2360. if (src->i_sb != dst->i_sb)
  2361. goto next;
  2362. if (S_ISDIR(dst->i_mode)) {
  2363. info.status = -EISDIR;
  2364. goto next;
  2365. }
  2366. if (!S_ISREG(dst->i_mode)) {
  2367. info.status = -EACCES;
  2368. goto next;
  2369. }
  2370. info.status = btrfs_extent_same(src, off, len, dst,
  2371. info.logical_offset);
  2372. if (info.status == 0)
  2373. info.bytes_deduped += len;
  2374. next:
  2375. if (dst_file)
  2376. fput(dst_file);
  2377. if (__put_user_unaligned(info.status, &args->info[i].status) ||
  2378. __put_user_unaligned(info.bytes_deduped,
  2379. &args->info[i].bytes_deduped)) {
  2380. ret = -EFAULT;
  2381. goto out;
  2382. }
  2383. }
  2384. out:
  2385. mnt_drop_write_file(file);
  2386. return ret;
  2387. }
  2388. /**
  2389. * btrfs_clone() - clone a range from inode file to another
  2390. *
  2391. * @src: Inode to clone from
  2392. * @inode: Inode to clone to
  2393. * @off: Offset within source to start clone from
  2394. * @olen: Original length, passed by user, of range to clone
  2395. * @olen_aligned: Block-aligned value of olen, extent_same uses
  2396. * identical values here
  2397. * @destoff: Offset within @inode to start clone
  2398. */
  2399. static int btrfs_clone(struct inode *src, struct inode *inode,
  2400. u64 off, u64 olen, u64 olen_aligned, u64 destoff)
  2401. {
  2402. struct btrfs_root *root = BTRFS_I(inode)->root;
  2403. struct btrfs_path *path = NULL;
  2404. struct extent_buffer *leaf;
  2405. struct btrfs_trans_handle *trans;
  2406. char *buf = NULL;
  2407. struct btrfs_key key;
  2408. u32 nritems;
  2409. int slot;
  2410. int ret;
  2411. u64 len = olen_aligned;
  2412. ret = -ENOMEM;
  2413. buf = vmalloc(btrfs_level_size(root, 0));
  2414. if (!buf)
  2415. return ret;
  2416. path = btrfs_alloc_path();
  2417. if (!path) {
  2418. vfree(buf);
  2419. return ret;
  2420. }
  2421. path->reada = 2;
  2422. /* clone data */
  2423. key.objectid = btrfs_ino(src);
  2424. key.type = BTRFS_EXTENT_DATA_KEY;
  2425. key.offset = 0;
  2426. while (1) {
  2427. /*
  2428. * note the key will change type as we walk through the
  2429. * tree.
  2430. */
  2431. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2432. 0, 0);
  2433. if (ret < 0)
  2434. goto out;
  2435. nritems = btrfs_header_nritems(path->nodes[0]);
  2436. if (path->slots[0] >= nritems) {
  2437. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2438. if (ret < 0)
  2439. goto out;
  2440. if (ret > 0)
  2441. break;
  2442. nritems = btrfs_header_nritems(path->nodes[0]);
  2443. }
  2444. leaf = path->nodes[0];
  2445. slot = path->slots[0];
  2446. btrfs_item_key_to_cpu(leaf, &key, slot);
  2447. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2448. key.objectid != btrfs_ino(src))
  2449. break;
  2450. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2451. struct btrfs_file_extent_item *extent;
  2452. int type;
  2453. u32 size;
  2454. struct btrfs_key new_key;
  2455. u64 disko = 0, diskl = 0;
  2456. u64 datao = 0, datal = 0;
  2457. u8 comp;
  2458. u64 endoff;
  2459. size = btrfs_item_size_nr(leaf, slot);
  2460. read_extent_buffer(leaf, buf,
  2461. btrfs_item_ptr_offset(leaf, slot),
  2462. size);
  2463. extent = btrfs_item_ptr(leaf, slot,
  2464. struct btrfs_file_extent_item);
  2465. comp = btrfs_file_extent_compression(leaf, extent);
  2466. type = btrfs_file_extent_type(leaf, extent);
  2467. if (type == BTRFS_FILE_EXTENT_REG ||
  2468. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2469. disko = btrfs_file_extent_disk_bytenr(leaf,
  2470. extent);
  2471. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2472. extent);
  2473. datao = btrfs_file_extent_offset(leaf, extent);
  2474. datal = btrfs_file_extent_num_bytes(leaf,
  2475. extent);
  2476. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2477. /* take upper bound, may be compressed */
  2478. datal = btrfs_file_extent_ram_bytes(leaf,
  2479. extent);
  2480. }
  2481. btrfs_release_path(path);
  2482. if (key.offset + datal <= off ||
  2483. key.offset >= off + len - 1)
  2484. goto next;
  2485. memcpy(&new_key, &key, sizeof(new_key));
  2486. new_key.objectid = btrfs_ino(inode);
  2487. if (off <= key.offset)
  2488. new_key.offset = key.offset + destoff - off;
  2489. else
  2490. new_key.offset = destoff;
  2491. /*
  2492. * 1 - adjusting old extent (we may have to split it)
  2493. * 1 - add new extent
  2494. * 1 - inode update
  2495. */
  2496. trans = btrfs_start_transaction(root, 3);
  2497. if (IS_ERR(trans)) {
  2498. ret = PTR_ERR(trans);
  2499. goto out;
  2500. }
  2501. if (type == BTRFS_FILE_EXTENT_REG ||
  2502. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2503. /*
  2504. * a | --- range to clone ---| b
  2505. * | ------------- extent ------------- |
  2506. */
  2507. /* substract range b */
  2508. if (key.offset + datal > off + len)
  2509. datal = off + len - key.offset;
  2510. /* substract range a */
  2511. if (off > key.offset) {
  2512. datao += off - key.offset;
  2513. datal -= off - key.offset;
  2514. }
  2515. ret = btrfs_drop_extents(trans, root, inode,
  2516. new_key.offset,
  2517. new_key.offset + datal,
  2518. 1);
  2519. if (ret) {
  2520. btrfs_abort_transaction(trans, root,
  2521. ret);
  2522. btrfs_end_transaction(trans, root);
  2523. goto out;
  2524. }
  2525. ret = btrfs_insert_empty_item(trans, root, path,
  2526. &new_key, size);
  2527. if (ret) {
  2528. btrfs_abort_transaction(trans, root,
  2529. ret);
  2530. btrfs_end_transaction(trans, root);
  2531. goto out;
  2532. }
  2533. leaf = path->nodes[0];
  2534. slot = path->slots[0];
  2535. write_extent_buffer(leaf, buf,
  2536. btrfs_item_ptr_offset(leaf, slot),
  2537. size);
  2538. extent = btrfs_item_ptr(leaf, slot,
  2539. struct btrfs_file_extent_item);
  2540. /* disko == 0 means it's a hole */
  2541. if (!disko)
  2542. datao = 0;
  2543. btrfs_set_file_extent_offset(leaf, extent,
  2544. datao);
  2545. btrfs_set_file_extent_num_bytes(leaf, extent,
  2546. datal);
  2547. if (disko) {
  2548. inode_add_bytes(inode, datal);
  2549. ret = btrfs_inc_extent_ref(trans, root,
  2550. disko, diskl, 0,
  2551. root->root_key.objectid,
  2552. btrfs_ino(inode),
  2553. new_key.offset - datao,
  2554. 0);
  2555. if (ret) {
  2556. btrfs_abort_transaction(trans,
  2557. root,
  2558. ret);
  2559. btrfs_end_transaction(trans,
  2560. root);
  2561. goto out;
  2562. }
  2563. }
  2564. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2565. u64 skip = 0;
  2566. u64 trim = 0;
  2567. if (off > key.offset) {
  2568. skip = off - key.offset;
  2569. new_key.offset += skip;
  2570. }
  2571. if (key.offset + datal > off + len)
  2572. trim = key.offset + datal - (off + len);
  2573. if (comp && (skip || trim)) {
  2574. ret = -EINVAL;
  2575. btrfs_end_transaction(trans, root);
  2576. goto out;
  2577. }
  2578. size -= skip + trim;
  2579. datal -= skip + trim;
  2580. ret = btrfs_drop_extents(trans, root, inode,
  2581. new_key.offset,
  2582. new_key.offset + datal,
  2583. 1);
  2584. if (ret) {
  2585. btrfs_abort_transaction(trans, root,
  2586. ret);
  2587. btrfs_end_transaction(trans, root);
  2588. goto out;
  2589. }
  2590. ret = btrfs_insert_empty_item(trans, root, path,
  2591. &new_key, size);
  2592. if (ret) {
  2593. btrfs_abort_transaction(trans, root,
  2594. ret);
  2595. btrfs_end_transaction(trans, root);
  2596. goto out;
  2597. }
  2598. if (skip) {
  2599. u32 start =
  2600. btrfs_file_extent_calc_inline_size(0);
  2601. memmove(buf+start, buf+start+skip,
  2602. datal);
  2603. }
  2604. leaf = path->nodes[0];
  2605. slot = path->slots[0];
  2606. write_extent_buffer(leaf, buf,
  2607. btrfs_item_ptr_offset(leaf, slot),
  2608. size);
  2609. inode_add_bytes(inode, datal);
  2610. }
  2611. btrfs_mark_buffer_dirty(leaf);
  2612. btrfs_release_path(path);
  2613. inode_inc_iversion(inode);
  2614. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2615. /*
  2616. * we round up to the block size at eof when
  2617. * determining which extents to clone above,
  2618. * but shouldn't round up the file size
  2619. */
  2620. endoff = new_key.offset + datal;
  2621. if (endoff > destoff+olen)
  2622. endoff = destoff+olen;
  2623. if (endoff > inode->i_size)
  2624. btrfs_i_size_write(inode, endoff);
  2625. ret = btrfs_update_inode(trans, root, inode);
  2626. if (ret) {
  2627. btrfs_abort_transaction(trans, root, ret);
  2628. btrfs_end_transaction(trans, root);
  2629. goto out;
  2630. }
  2631. ret = btrfs_end_transaction(trans, root);
  2632. }
  2633. next:
  2634. btrfs_release_path(path);
  2635. key.offset++;
  2636. }
  2637. ret = 0;
  2638. out:
  2639. btrfs_release_path(path);
  2640. btrfs_free_path(path);
  2641. vfree(buf);
  2642. return ret;
  2643. }
  2644. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2645. u64 off, u64 olen, u64 destoff)
  2646. {
  2647. struct inode *inode = fdentry(file)->d_inode;
  2648. struct btrfs_root *root = BTRFS_I(inode)->root;
  2649. struct fd src_file;
  2650. struct inode *src;
  2651. int ret;
  2652. u64 len = olen;
  2653. u64 bs = root->fs_info->sb->s_blocksize;
  2654. int same_inode = 0;
  2655. /*
  2656. * TODO:
  2657. * - split compressed inline extents. annoying: we need to
  2658. * decompress into destination's address_space (the file offset
  2659. * may change, so source mapping won't do), then recompress (or
  2660. * otherwise reinsert) a subrange.
  2661. * - allow ranges within the same file to be cloned (provided
  2662. * they don't overlap)?
  2663. */
  2664. /* the destination must be opened for writing */
  2665. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  2666. return -EINVAL;
  2667. if (btrfs_root_readonly(root))
  2668. return -EROFS;
  2669. ret = mnt_want_write_file(file);
  2670. if (ret)
  2671. return ret;
  2672. src_file = fdget(srcfd);
  2673. if (!src_file.file) {
  2674. ret = -EBADF;
  2675. goto out_drop_write;
  2676. }
  2677. ret = -EXDEV;
  2678. if (src_file.file->f_path.mnt != file->f_path.mnt)
  2679. goto out_fput;
  2680. src = file_inode(src_file.file);
  2681. ret = -EINVAL;
  2682. if (src == inode)
  2683. same_inode = 1;
  2684. /* the src must be open for reading */
  2685. if (!(src_file.file->f_mode & FMODE_READ))
  2686. goto out_fput;
  2687. /* don't make the dst file partly checksummed */
  2688. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2689. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  2690. goto out_fput;
  2691. ret = -EISDIR;
  2692. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  2693. goto out_fput;
  2694. ret = -EXDEV;
  2695. if (src->i_sb != inode->i_sb)
  2696. goto out_fput;
  2697. if (!same_inode) {
  2698. if (inode < src) {
  2699. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2700. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2701. } else {
  2702. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2703. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2704. }
  2705. } else {
  2706. mutex_lock(&src->i_mutex);
  2707. }
  2708. /* determine range to clone */
  2709. ret = -EINVAL;
  2710. if (off + len > src->i_size || off + len < off)
  2711. goto out_unlock;
  2712. if (len == 0)
  2713. olen = len = src->i_size - off;
  2714. /* if we extend to eof, continue to block boundary */
  2715. if (off + len == src->i_size)
  2716. len = ALIGN(src->i_size, bs) - off;
  2717. /* verify the end result is block aligned */
  2718. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2719. !IS_ALIGNED(destoff, bs))
  2720. goto out_unlock;
  2721. /* verify if ranges are overlapped within the same file */
  2722. if (same_inode) {
  2723. if (destoff + len > off && destoff < off + len)
  2724. goto out_unlock;
  2725. }
  2726. if (destoff > inode->i_size) {
  2727. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2728. if (ret)
  2729. goto out_unlock;
  2730. }
  2731. /* truncate page cache pages from target inode range */
  2732. truncate_inode_pages_range(&inode->i_data, destoff,
  2733. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2734. lock_extent_range(src, off, len);
  2735. ret = btrfs_clone(src, inode, off, olen, len, destoff);
  2736. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2737. out_unlock:
  2738. mutex_unlock(&src->i_mutex);
  2739. if (!same_inode)
  2740. mutex_unlock(&inode->i_mutex);
  2741. out_fput:
  2742. fdput(src_file);
  2743. out_drop_write:
  2744. mnt_drop_write_file(file);
  2745. return ret;
  2746. }
  2747. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2748. {
  2749. struct btrfs_ioctl_clone_range_args args;
  2750. if (copy_from_user(&args, argp, sizeof(args)))
  2751. return -EFAULT;
  2752. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2753. args.src_length, args.dest_offset);
  2754. }
  2755. /*
  2756. * there are many ways the trans_start and trans_end ioctls can lead
  2757. * to deadlocks. They should only be used by applications that
  2758. * basically own the machine, and have a very in depth understanding
  2759. * of all the possible deadlocks and enospc problems.
  2760. */
  2761. static long btrfs_ioctl_trans_start(struct file *file)
  2762. {
  2763. struct inode *inode = file_inode(file);
  2764. struct btrfs_root *root = BTRFS_I(inode)->root;
  2765. struct btrfs_trans_handle *trans;
  2766. int ret;
  2767. ret = -EPERM;
  2768. if (!capable(CAP_SYS_ADMIN))
  2769. goto out;
  2770. ret = -EINPROGRESS;
  2771. if (file->private_data)
  2772. goto out;
  2773. ret = -EROFS;
  2774. if (btrfs_root_readonly(root))
  2775. goto out;
  2776. ret = mnt_want_write_file(file);
  2777. if (ret)
  2778. goto out;
  2779. atomic_inc(&root->fs_info->open_ioctl_trans);
  2780. ret = -ENOMEM;
  2781. trans = btrfs_start_ioctl_transaction(root);
  2782. if (IS_ERR(trans))
  2783. goto out_drop;
  2784. file->private_data = trans;
  2785. return 0;
  2786. out_drop:
  2787. atomic_dec(&root->fs_info->open_ioctl_trans);
  2788. mnt_drop_write_file(file);
  2789. out:
  2790. return ret;
  2791. }
  2792. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2793. {
  2794. struct inode *inode = file_inode(file);
  2795. struct btrfs_root *root = BTRFS_I(inode)->root;
  2796. struct btrfs_root *new_root;
  2797. struct btrfs_dir_item *di;
  2798. struct btrfs_trans_handle *trans;
  2799. struct btrfs_path *path;
  2800. struct btrfs_key location;
  2801. struct btrfs_disk_key disk_key;
  2802. u64 objectid = 0;
  2803. u64 dir_id;
  2804. int ret;
  2805. if (!capable(CAP_SYS_ADMIN))
  2806. return -EPERM;
  2807. ret = mnt_want_write_file(file);
  2808. if (ret)
  2809. return ret;
  2810. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2811. ret = -EFAULT;
  2812. goto out;
  2813. }
  2814. if (!objectid)
  2815. objectid = root->root_key.objectid;
  2816. location.objectid = objectid;
  2817. location.type = BTRFS_ROOT_ITEM_KEY;
  2818. location.offset = (u64)-1;
  2819. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2820. if (IS_ERR(new_root)) {
  2821. ret = PTR_ERR(new_root);
  2822. goto out;
  2823. }
  2824. path = btrfs_alloc_path();
  2825. if (!path) {
  2826. ret = -ENOMEM;
  2827. goto out;
  2828. }
  2829. path->leave_spinning = 1;
  2830. trans = btrfs_start_transaction(root, 1);
  2831. if (IS_ERR(trans)) {
  2832. btrfs_free_path(path);
  2833. ret = PTR_ERR(trans);
  2834. goto out;
  2835. }
  2836. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2837. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2838. dir_id, "default", 7, 1);
  2839. if (IS_ERR_OR_NULL(di)) {
  2840. btrfs_free_path(path);
  2841. btrfs_end_transaction(trans, root);
  2842. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2843. "this isn't going to work\n");
  2844. ret = -ENOENT;
  2845. goto out;
  2846. }
  2847. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2848. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2849. btrfs_mark_buffer_dirty(path->nodes[0]);
  2850. btrfs_free_path(path);
  2851. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2852. btrfs_end_transaction(trans, root);
  2853. out:
  2854. mnt_drop_write_file(file);
  2855. return ret;
  2856. }
  2857. void btrfs_get_block_group_info(struct list_head *groups_list,
  2858. struct btrfs_ioctl_space_info *space)
  2859. {
  2860. struct btrfs_block_group_cache *block_group;
  2861. space->total_bytes = 0;
  2862. space->used_bytes = 0;
  2863. space->flags = 0;
  2864. list_for_each_entry(block_group, groups_list, list) {
  2865. space->flags = block_group->flags;
  2866. space->total_bytes += block_group->key.offset;
  2867. space->used_bytes +=
  2868. btrfs_block_group_used(&block_group->item);
  2869. }
  2870. }
  2871. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2872. {
  2873. struct btrfs_ioctl_space_args space_args;
  2874. struct btrfs_ioctl_space_info space;
  2875. struct btrfs_ioctl_space_info *dest;
  2876. struct btrfs_ioctl_space_info *dest_orig;
  2877. struct btrfs_ioctl_space_info __user *user_dest;
  2878. struct btrfs_space_info *info;
  2879. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2880. BTRFS_BLOCK_GROUP_SYSTEM,
  2881. BTRFS_BLOCK_GROUP_METADATA,
  2882. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2883. int num_types = 4;
  2884. int alloc_size;
  2885. int ret = 0;
  2886. u64 slot_count = 0;
  2887. int i, c;
  2888. if (copy_from_user(&space_args,
  2889. (struct btrfs_ioctl_space_args __user *)arg,
  2890. sizeof(space_args)))
  2891. return -EFAULT;
  2892. for (i = 0; i < num_types; i++) {
  2893. struct btrfs_space_info *tmp;
  2894. info = NULL;
  2895. rcu_read_lock();
  2896. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2897. list) {
  2898. if (tmp->flags == types[i]) {
  2899. info = tmp;
  2900. break;
  2901. }
  2902. }
  2903. rcu_read_unlock();
  2904. if (!info)
  2905. continue;
  2906. down_read(&info->groups_sem);
  2907. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2908. if (!list_empty(&info->block_groups[c]))
  2909. slot_count++;
  2910. }
  2911. up_read(&info->groups_sem);
  2912. }
  2913. /* space_slots == 0 means they are asking for a count */
  2914. if (space_args.space_slots == 0) {
  2915. space_args.total_spaces = slot_count;
  2916. goto out;
  2917. }
  2918. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2919. alloc_size = sizeof(*dest) * slot_count;
  2920. /* we generally have at most 6 or so space infos, one for each raid
  2921. * level. So, a whole page should be more than enough for everyone
  2922. */
  2923. if (alloc_size > PAGE_CACHE_SIZE)
  2924. return -ENOMEM;
  2925. space_args.total_spaces = 0;
  2926. dest = kmalloc(alloc_size, GFP_NOFS);
  2927. if (!dest)
  2928. return -ENOMEM;
  2929. dest_orig = dest;
  2930. /* now we have a buffer to copy into */
  2931. for (i = 0; i < num_types; i++) {
  2932. struct btrfs_space_info *tmp;
  2933. if (!slot_count)
  2934. break;
  2935. info = NULL;
  2936. rcu_read_lock();
  2937. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2938. list) {
  2939. if (tmp->flags == types[i]) {
  2940. info = tmp;
  2941. break;
  2942. }
  2943. }
  2944. rcu_read_unlock();
  2945. if (!info)
  2946. continue;
  2947. down_read(&info->groups_sem);
  2948. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2949. if (!list_empty(&info->block_groups[c])) {
  2950. btrfs_get_block_group_info(
  2951. &info->block_groups[c], &space);
  2952. memcpy(dest, &space, sizeof(space));
  2953. dest++;
  2954. space_args.total_spaces++;
  2955. slot_count--;
  2956. }
  2957. if (!slot_count)
  2958. break;
  2959. }
  2960. up_read(&info->groups_sem);
  2961. }
  2962. user_dest = (struct btrfs_ioctl_space_info __user *)
  2963. (arg + sizeof(struct btrfs_ioctl_space_args));
  2964. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2965. ret = -EFAULT;
  2966. kfree(dest_orig);
  2967. out:
  2968. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2969. ret = -EFAULT;
  2970. return ret;
  2971. }
  2972. /*
  2973. * there are many ways the trans_start and trans_end ioctls can lead
  2974. * to deadlocks. They should only be used by applications that
  2975. * basically own the machine, and have a very in depth understanding
  2976. * of all the possible deadlocks and enospc problems.
  2977. */
  2978. long btrfs_ioctl_trans_end(struct file *file)
  2979. {
  2980. struct inode *inode = file_inode(file);
  2981. struct btrfs_root *root = BTRFS_I(inode)->root;
  2982. struct btrfs_trans_handle *trans;
  2983. trans = file->private_data;
  2984. if (!trans)
  2985. return -EINVAL;
  2986. file->private_data = NULL;
  2987. btrfs_end_transaction(trans, root);
  2988. atomic_dec(&root->fs_info->open_ioctl_trans);
  2989. mnt_drop_write_file(file);
  2990. return 0;
  2991. }
  2992. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  2993. void __user *argp)
  2994. {
  2995. struct btrfs_trans_handle *trans;
  2996. u64 transid;
  2997. int ret;
  2998. trans = btrfs_attach_transaction_barrier(root);
  2999. if (IS_ERR(trans)) {
  3000. if (PTR_ERR(trans) != -ENOENT)
  3001. return PTR_ERR(trans);
  3002. /* No running transaction, don't bother */
  3003. transid = root->fs_info->last_trans_committed;
  3004. goto out;
  3005. }
  3006. transid = trans->transid;
  3007. ret = btrfs_commit_transaction_async(trans, root, 0);
  3008. if (ret) {
  3009. btrfs_end_transaction(trans, root);
  3010. return ret;
  3011. }
  3012. out:
  3013. if (argp)
  3014. if (copy_to_user(argp, &transid, sizeof(transid)))
  3015. return -EFAULT;
  3016. return 0;
  3017. }
  3018. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  3019. void __user *argp)
  3020. {
  3021. u64 transid;
  3022. if (argp) {
  3023. if (copy_from_user(&transid, argp, sizeof(transid)))
  3024. return -EFAULT;
  3025. } else {
  3026. transid = 0; /* current trans */
  3027. }
  3028. return btrfs_wait_for_commit(root, transid);
  3029. }
  3030. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  3031. {
  3032. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3033. struct btrfs_ioctl_scrub_args *sa;
  3034. int ret;
  3035. if (!capable(CAP_SYS_ADMIN))
  3036. return -EPERM;
  3037. sa = memdup_user(arg, sizeof(*sa));
  3038. if (IS_ERR(sa))
  3039. return PTR_ERR(sa);
  3040. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  3041. ret = mnt_want_write_file(file);
  3042. if (ret)
  3043. goto out;
  3044. }
  3045. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  3046. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  3047. 0);
  3048. if (copy_to_user(arg, sa, sizeof(*sa)))
  3049. ret = -EFAULT;
  3050. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  3051. mnt_drop_write_file(file);
  3052. out:
  3053. kfree(sa);
  3054. return ret;
  3055. }
  3056. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  3057. {
  3058. if (!capable(CAP_SYS_ADMIN))
  3059. return -EPERM;
  3060. return btrfs_scrub_cancel(root->fs_info);
  3061. }
  3062. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  3063. void __user *arg)
  3064. {
  3065. struct btrfs_ioctl_scrub_args *sa;
  3066. int ret;
  3067. if (!capable(CAP_SYS_ADMIN))
  3068. return -EPERM;
  3069. sa = memdup_user(arg, sizeof(*sa));
  3070. if (IS_ERR(sa))
  3071. return PTR_ERR(sa);
  3072. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  3073. if (copy_to_user(arg, sa, sizeof(*sa)))
  3074. ret = -EFAULT;
  3075. kfree(sa);
  3076. return ret;
  3077. }
  3078. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  3079. void __user *arg)
  3080. {
  3081. struct btrfs_ioctl_get_dev_stats *sa;
  3082. int ret;
  3083. sa = memdup_user(arg, sizeof(*sa));
  3084. if (IS_ERR(sa))
  3085. return PTR_ERR(sa);
  3086. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  3087. kfree(sa);
  3088. return -EPERM;
  3089. }
  3090. ret = btrfs_get_dev_stats(root, sa);
  3091. if (copy_to_user(arg, sa, sizeof(*sa)))
  3092. ret = -EFAULT;
  3093. kfree(sa);
  3094. return ret;
  3095. }
  3096. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  3097. {
  3098. struct btrfs_ioctl_dev_replace_args *p;
  3099. int ret;
  3100. if (!capable(CAP_SYS_ADMIN))
  3101. return -EPERM;
  3102. p = memdup_user(arg, sizeof(*p));
  3103. if (IS_ERR(p))
  3104. return PTR_ERR(p);
  3105. switch (p->cmd) {
  3106. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  3107. if (atomic_xchg(
  3108. &root->fs_info->mutually_exclusive_operation_running,
  3109. 1)) {
  3110. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  3111. ret = -EINPROGRESS;
  3112. } else {
  3113. ret = btrfs_dev_replace_start(root, p);
  3114. atomic_set(
  3115. &root->fs_info->mutually_exclusive_operation_running,
  3116. 0);
  3117. }
  3118. break;
  3119. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  3120. btrfs_dev_replace_status(root->fs_info, p);
  3121. ret = 0;
  3122. break;
  3123. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  3124. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  3125. break;
  3126. default:
  3127. ret = -EINVAL;
  3128. break;
  3129. }
  3130. if (copy_to_user(arg, p, sizeof(*p)))
  3131. ret = -EFAULT;
  3132. kfree(p);
  3133. return ret;
  3134. }
  3135. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  3136. {
  3137. int ret = 0;
  3138. int i;
  3139. u64 rel_ptr;
  3140. int size;
  3141. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  3142. struct inode_fs_paths *ipath = NULL;
  3143. struct btrfs_path *path;
  3144. if (!capable(CAP_DAC_READ_SEARCH))
  3145. return -EPERM;
  3146. path = btrfs_alloc_path();
  3147. if (!path) {
  3148. ret = -ENOMEM;
  3149. goto out;
  3150. }
  3151. ipa = memdup_user(arg, sizeof(*ipa));
  3152. if (IS_ERR(ipa)) {
  3153. ret = PTR_ERR(ipa);
  3154. ipa = NULL;
  3155. goto out;
  3156. }
  3157. size = min_t(u32, ipa->size, 4096);
  3158. ipath = init_ipath(size, root, path);
  3159. if (IS_ERR(ipath)) {
  3160. ret = PTR_ERR(ipath);
  3161. ipath = NULL;
  3162. goto out;
  3163. }
  3164. ret = paths_from_inode(ipa->inum, ipath);
  3165. if (ret < 0)
  3166. goto out;
  3167. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  3168. rel_ptr = ipath->fspath->val[i] -
  3169. (u64)(unsigned long)ipath->fspath->val;
  3170. ipath->fspath->val[i] = rel_ptr;
  3171. }
  3172. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  3173. (void *)(unsigned long)ipath->fspath, size);
  3174. if (ret) {
  3175. ret = -EFAULT;
  3176. goto out;
  3177. }
  3178. out:
  3179. btrfs_free_path(path);
  3180. free_ipath(ipath);
  3181. kfree(ipa);
  3182. return ret;
  3183. }
  3184. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  3185. {
  3186. struct btrfs_data_container *inodes = ctx;
  3187. const size_t c = 3 * sizeof(u64);
  3188. if (inodes->bytes_left >= c) {
  3189. inodes->bytes_left -= c;
  3190. inodes->val[inodes->elem_cnt] = inum;
  3191. inodes->val[inodes->elem_cnt + 1] = offset;
  3192. inodes->val[inodes->elem_cnt + 2] = root;
  3193. inodes->elem_cnt += 3;
  3194. } else {
  3195. inodes->bytes_missing += c - inodes->bytes_left;
  3196. inodes->bytes_left = 0;
  3197. inodes->elem_missed += 3;
  3198. }
  3199. return 0;
  3200. }
  3201. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  3202. void __user *arg)
  3203. {
  3204. int ret = 0;
  3205. int size;
  3206. struct btrfs_ioctl_logical_ino_args *loi;
  3207. struct btrfs_data_container *inodes = NULL;
  3208. struct btrfs_path *path = NULL;
  3209. if (!capable(CAP_SYS_ADMIN))
  3210. return -EPERM;
  3211. loi = memdup_user(arg, sizeof(*loi));
  3212. if (IS_ERR(loi)) {
  3213. ret = PTR_ERR(loi);
  3214. loi = NULL;
  3215. goto out;
  3216. }
  3217. path = btrfs_alloc_path();
  3218. if (!path) {
  3219. ret = -ENOMEM;
  3220. goto out;
  3221. }
  3222. size = min_t(u32, loi->size, 64 * 1024);
  3223. inodes = init_data_container(size);
  3224. if (IS_ERR(inodes)) {
  3225. ret = PTR_ERR(inodes);
  3226. inodes = NULL;
  3227. goto out;
  3228. }
  3229. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  3230. build_ino_list, inodes);
  3231. if (ret == -EINVAL)
  3232. ret = -ENOENT;
  3233. if (ret < 0)
  3234. goto out;
  3235. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  3236. (void *)(unsigned long)inodes, size);
  3237. if (ret)
  3238. ret = -EFAULT;
  3239. out:
  3240. btrfs_free_path(path);
  3241. vfree(inodes);
  3242. kfree(loi);
  3243. return ret;
  3244. }
  3245. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  3246. struct btrfs_ioctl_balance_args *bargs)
  3247. {
  3248. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3249. bargs->flags = bctl->flags;
  3250. if (atomic_read(&fs_info->balance_running))
  3251. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  3252. if (atomic_read(&fs_info->balance_pause_req))
  3253. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  3254. if (atomic_read(&fs_info->balance_cancel_req))
  3255. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  3256. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  3257. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  3258. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  3259. if (lock) {
  3260. spin_lock(&fs_info->balance_lock);
  3261. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3262. spin_unlock(&fs_info->balance_lock);
  3263. } else {
  3264. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3265. }
  3266. }
  3267. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  3268. {
  3269. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3270. struct btrfs_fs_info *fs_info = root->fs_info;
  3271. struct btrfs_ioctl_balance_args *bargs;
  3272. struct btrfs_balance_control *bctl;
  3273. bool need_unlock; /* for mut. excl. ops lock */
  3274. int ret;
  3275. if (!capable(CAP_SYS_ADMIN))
  3276. return -EPERM;
  3277. ret = mnt_want_write_file(file);
  3278. if (ret)
  3279. return ret;
  3280. again:
  3281. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  3282. mutex_lock(&fs_info->volume_mutex);
  3283. mutex_lock(&fs_info->balance_mutex);
  3284. need_unlock = true;
  3285. goto locked;
  3286. }
  3287. /*
  3288. * mut. excl. ops lock is locked. Three possibilites:
  3289. * (1) some other op is running
  3290. * (2) balance is running
  3291. * (3) balance is paused -- special case (think resume)
  3292. */
  3293. mutex_lock(&fs_info->balance_mutex);
  3294. if (fs_info->balance_ctl) {
  3295. /* this is either (2) or (3) */
  3296. if (!atomic_read(&fs_info->balance_running)) {
  3297. mutex_unlock(&fs_info->balance_mutex);
  3298. if (!mutex_trylock(&fs_info->volume_mutex))
  3299. goto again;
  3300. mutex_lock(&fs_info->balance_mutex);
  3301. if (fs_info->balance_ctl &&
  3302. !atomic_read(&fs_info->balance_running)) {
  3303. /* this is (3) */
  3304. need_unlock = false;
  3305. goto locked;
  3306. }
  3307. mutex_unlock(&fs_info->balance_mutex);
  3308. mutex_unlock(&fs_info->volume_mutex);
  3309. goto again;
  3310. } else {
  3311. /* this is (2) */
  3312. mutex_unlock(&fs_info->balance_mutex);
  3313. ret = -EINPROGRESS;
  3314. goto out;
  3315. }
  3316. } else {
  3317. /* this is (1) */
  3318. mutex_unlock(&fs_info->balance_mutex);
  3319. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  3320. ret = -EINVAL;
  3321. goto out;
  3322. }
  3323. locked:
  3324. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3325. if (arg) {
  3326. bargs = memdup_user(arg, sizeof(*bargs));
  3327. if (IS_ERR(bargs)) {
  3328. ret = PTR_ERR(bargs);
  3329. goto out_unlock;
  3330. }
  3331. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3332. if (!fs_info->balance_ctl) {
  3333. ret = -ENOTCONN;
  3334. goto out_bargs;
  3335. }
  3336. bctl = fs_info->balance_ctl;
  3337. spin_lock(&fs_info->balance_lock);
  3338. bctl->flags |= BTRFS_BALANCE_RESUME;
  3339. spin_unlock(&fs_info->balance_lock);
  3340. goto do_balance;
  3341. }
  3342. } else {
  3343. bargs = NULL;
  3344. }
  3345. if (fs_info->balance_ctl) {
  3346. ret = -EINPROGRESS;
  3347. goto out_bargs;
  3348. }
  3349. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3350. if (!bctl) {
  3351. ret = -ENOMEM;
  3352. goto out_bargs;
  3353. }
  3354. bctl->fs_info = fs_info;
  3355. if (arg) {
  3356. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3357. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3358. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3359. bctl->flags = bargs->flags;
  3360. } else {
  3361. /* balance everything - no filters */
  3362. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3363. }
  3364. do_balance:
  3365. /*
  3366. * Ownership of bctl and mutually_exclusive_operation_running
  3367. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3368. * or, if restriper was paused all the way until unmount, in
  3369. * free_fs_info. mutually_exclusive_operation_running is
  3370. * cleared in __cancel_balance.
  3371. */
  3372. need_unlock = false;
  3373. ret = btrfs_balance(bctl, bargs);
  3374. if (arg) {
  3375. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3376. ret = -EFAULT;
  3377. }
  3378. out_bargs:
  3379. kfree(bargs);
  3380. out_unlock:
  3381. mutex_unlock(&fs_info->balance_mutex);
  3382. mutex_unlock(&fs_info->volume_mutex);
  3383. if (need_unlock)
  3384. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3385. out:
  3386. mnt_drop_write_file(file);
  3387. return ret;
  3388. }
  3389. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3390. {
  3391. if (!capable(CAP_SYS_ADMIN))
  3392. return -EPERM;
  3393. switch (cmd) {
  3394. case BTRFS_BALANCE_CTL_PAUSE:
  3395. return btrfs_pause_balance(root->fs_info);
  3396. case BTRFS_BALANCE_CTL_CANCEL:
  3397. return btrfs_cancel_balance(root->fs_info);
  3398. }
  3399. return -EINVAL;
  3400. }
  3401. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3402. void __user *arg)
  3403. {
  3404. struct btrfs_fs_info *fs_info = root->fs_info;
  3405. struct btrfs_ioctl_balance_args *bargs;
  3406. int ret = 0;
  3407. if (!capable(CAP_SYS_ADMIN))
  3408. return -EPERM;
  3409. mutex_lock(&fs_info->balance_mutex);
  3410. if (!fs_info->balance_ctl) {
  3411. ret = -ENOTCONN;
  3412. goto out;
  3413. }
  3414. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3415. if (!bargs) {
  3416. ret = -ENOMEM;
  3417. goto out;
  3418. }
  3419. update_ioctl_balance_args(fs_info, 1, bargs);
  3420. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3421. ret = -EFAULT;
  3422. kfree(bargs);
  3423. out:
  3424. mutex_unlock(&fs_info->balance_mutex);
  3425. return ret;
  3426. }
  3427. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3428. {
  3429. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3430. struct btrfs_ioctl_quota_ctl_args *sa;
  3431. struct btrfs_trans_handle *trans = NULL;
  3432. int ret;
  3433. int err;
  3434. if (!capable(CAP_SYS_ADMIN))
  3435. return -EPERM;
  3436. ret = mnt_want_write_file(file);
  3437. if (ret)
  3438. return ret;
  3439. sa = memdup_user(arg, sizeof(*sa));
  3440. if (IS_ERR(sa)) {
  3441. ret = PTR_ERR(sa);
  3442. goto drop_write;
  3443. }
  3444. down_write(&root->fs_info->subvol_sem);
  3445. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  3446. if (IS_ERR(trans)) {
  3447. ret = PTR_ERR(trans);
  3448. goto out;
  3449. }
  3450. switch (sa->cmd) {
  3451. case BTRFS_QUOTA_CTL_ENABLE:
  3452. ret = btrfs_quota_enable(trans, root->fs_info);
  3453. break;
  3454. case BTRFS_QUOTA_CTL_DISABLE:
  3455. ret = btrfs_quota_disable(trans, root->fs_info);
  3456. break;
  3457. default:
  3458. ret = -EINVAL;
  3459. break;
  3460. }
  3461. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  3462. if (err && !ret)
  3463. ret = err;
  3464. out:
  3465. kfree(sa);
  3466. up_write(&root->fs_info->subvol_sem);
  3467. drop_write:
  3468. mnt_drop_write_file(file);
  3469. return ret;
  3470. }
  3471. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3472. {
  3473. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3474. struct btrfs_ioctl_qgroup_assign_args *sa;
  3475. struct btrfs_trans_handle *trans;
  3476. int ret;
  3477. int err;
  3478. if (!capable(CAP_SYS_ADMIN))
  3479. return -EPERM;
  3480. ret = mnt_want_write_file(file);
  3481. if (ret)
  3482. return ret;
  3483. sa = memdup_user(arg, sizeof(*sa));
  3484. if (IS_ERR(sa)) {
  3485. ret = PTR_ERR(sa);
  3486. goto drop_write;
  3487. }
  3488. trans = btrfs_join_transaction(root);
  3489. if (IS_ERR(trans)) {
  3490. ret = PTR_ERR(trans);
  3491. goto out;
  3492. }
  3493. /* FIXME: check if the IDs really exist */
  3494. if (sa->assign) {
  3495. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3496. sa->src, sa->dst);
  3497. } else {
  3498. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3499. sa->src, sa->dst);
  3500. }
  3501. err = btrfs_end_transaction(trans, root);
  3502. if (err && !ret)
  3503. ret = err;
  3504. out:
  3505. kfree(sa);
  3506. drop_write:
  3507. mnt_drop_write_file(file);
  3508. return ret;
  3509. }
  3510. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3511. {
  3512. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3513. struct btrfs_ioctl_qgroup_create_args *sa;
  3514. struct btrfs_trans_handle *trans;
  3515. int ret;
  3516. int err;
  3517. if (!capable(CAP_SYS_ADMIN))
  3518. return -EPERM;
  3519. ret = mnt_want_write_file(file);
  3520. if (ret)
  3521. return ret;
  3522. sa = memdup_user(arg, sizeof(*sa));
  3523. if (IS_ERR(sa)) {
  3524. ret = PTR_ERR(sa);
  3525. goto drop_write;
  3526. }
  3527. if (!sa->qgroupid) {
  3528. ret = -EINVAL;
  3529. goto out;
  3530. }
  3531. trans = btrfs_join_transaction(root);
  3532. if (IS_ERR(trans)) {
  3533. ret = PTR_ERR(trans);
  3534. goto out;
  3535. }
  3536. /* FIXME: check if the IDs really exist */
  3537. if (sa->create) {
  3538. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3539. NULL);
  3540. } else {
  3541. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3542. }
  3543. err = btrfs_end_transaction(trans, root);
  3544. if (err && !ret)
  3545. ret = err;
  3546. out:
  3547. kfree(sa);
  3548. drop_write:
  3549. mnt_drop_write_file(file);
  3550. return ret;
  3551. }
  3552. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3553. {
  3554. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3555. struct btrfs_ioctl_qgroup_limit_args *sa;
  3556. struct btrfs_trans_handle *trans;
  3557. int ret;
  3558. int err;
  3559. u64 qgroupid;
  3560. if (!capable(CAP_SYS_ADMIN))
  3561. return -EPERM;
  3562. ret = mnt_want_write_file(file);
  3563. if (ret)
  3564. return ret;
  3565. sa = memdup_user(arg, sizeof(*sa));
  3566. if (IS_ERR(sa)) {
  3567. ret = PTR_ERR(sa);
  3568. goto drop_write;
  3569. }
  3570. trans = btrfs_join_transaction(root);
  3571. if (IS_ERR(trans)) {
  3572. ret = PTR_ERR(trans);
  3573. goto out;
  3574. }
  3575. qgroupid = sa->qgroupid;
  3576. if (!qgroupid) {
  3577. /* take the current subvol as qgroup */
  3578. qgroupid = root->root_key.objectid;
  3579. }
  3580. /* FIXME: check if the IDs really exist */
  3581. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3582. err = btrfs_end_transaction(trans, root);
  3583. if (err && !ret)
  3584. ret = err;
  3585. out:
  3586. kfree(sa);
  3587. drop_write:
  3588. mnt_drop_write_file(file);
  3589. return ret;
  3590. }
  3591. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  3592. {
  3593. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3594. struct btrfs_ioctl_quota_rescan_args *qsa;
  3595. int ret;
  3596. if (!capable(CAP_SYS_ADMIN))
  3597. return -EPERM;
  3598. ret = mnt_want_write_file(file);
  3599. if (ret)
  3600. return ret;
  3601. qsa = memdup_user(arg, sizeof(*qsa));
  3602. if (IS_ERR(qsa)) {
  3603. ret = PTR_ERR(qsa);
  3604. goto drop_write;
  3605. }
  3606. if (qsa->flags) {
  3607. ret = -EINVAL;
  3608. goto out;
  3609. }
  3610. ret = btrfs_qgroup_rescan(root->fs_info);
  3611. out:
  3612. kfree(qsa);
  3613. drop_write:
  3614. mnt_drop_write_file(file);
  3615. return ret;
  3616. }
  3617. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  3618. {
  3619. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3620. struct btrfs_ioctl_quota_rescan_args *qsa;
  3621. int ret = 0;
  3622. if (!capable(CAP_SYS_ADMIN))
  3623. return -EPERM;
  3624. qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
  3625. if (!qsa)
  3626. return -ENOMEM;
  3627. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3628. qsa->flags = 1;
  3629. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  3630. }
  3631. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  3632. ret = -EFAULT;
  3633. kfree(qsa);
  3634. return ret;
  3635. }
  3636. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  3637. {
  3638. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3639. if (!capable(CAP_SYS_ADMIN))
  3640. return -EPERM;
  3641. return btrfs_qgroup_wait_for_completion(root->fs_info);
  3642. }
  3643. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3644. void __user *arg)
  3645. {
  3646. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3647. struct inode *inode = file_inode(file);
  3648. struct btrfs_root *root = BTRFS_I(inode)->root;
  3649. struct btrfs_root_item *root_item = &root->root_item;
  3650. struct btrfs_trans_handle *trans;
  3651. struct timespec ct = CURRENT_TIME;
  3652. int ret = 0;
  3653. ret = mnt_want_write_file(file);
  3654. if (ret < 0)
  3655. return ret;
  3656. down_write(&root->fs_info->subvol_sem);
  3657. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3658. ret = -EINVAL;
  3659. goto out;
  3660. }
  3661. if (btrfs_root_readonly(root)) {
  3662. ret = -EROFS;
  3663. goto out;
  3664. }
  3665. if (!inode_owner_or_capable(inode)) {
  3666. ret = -EACCES;
  3667. goto out;
  3668. }
  3669. sa = memdup_user(arg, sizeof(*sa));
  3670. if (IS_ERR(sa)) {
  3671. ret = PTR_ERR(sa);
  3672. sa = NULL;
  3673. goto out;
  3674. }
  3675. trans = btrfs_start_transaction(root, 1);
  3676. if (IS_ERR(trans)) {
  3677. ret = PTR_ERR(trans);
  3678. trans = NULL;
  3679. goto out;
  3680. }
  3681. sa->rtransid = trans->transid;
  3682. sa->rtime.sec = ct.tv_sec;
  3683. sa->rtime.nsec = ct.tv_nsec;
  3684. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3685. btrfs_set_root_stransid(root_item, sa->stransid);
  3686. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3687. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  3688. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  3689. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  3690. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  3691. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3692. &root->root_key, &root->root_item);
  3693. if (ret < 0) {
  3694. btrfs_end_transaction(trans, root);
  3695. trans = NULL;
  3696. goto out;
  3697. } else {
  3698. ret = btrfs_commit_transaction(trans, root);
  3699. if (ret < 0)
  3700. goto out;
  3701. }
  3702. ret = copy_to_user(arg, sa, sizeof(*sa));
  3703. if (ret)
  3704. ret = -EFAULT;
  3705. out:
  3706. kfree(sa);
  3707. up_write(&root->fs_info->subvol_sem);
  3708. mnt_drop_write_file(file);
  3709. return ret;
  3710. }
  3711. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  3712. {
  3713. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3714. size_t len;
  3715. int ret;
  3716. char label[BTRFS_LABEL_SIZE];
  3717. spin_lock(&root->fs_info->super_lock);
  3718. memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  3719. spin_unlock(&root->fs_info->super_lock);
  3720. len = strnlen(label, BTRFS_LABEL_SIZE);
  3721. if (len == BTRFS_LABEL_SIZE) {
  3722. pr_warn("btrfs: label is too long, return the first %zu bytes\n",
  3723. --len);
  3724. }
  3725. ret = copy_to_user(arg, label, len);
  3726. return ret ? -EFAULT : 0;
  3727. }
  3728. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  3729. {
  3730. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3731. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  3732. struct btrfs_trans_handle *trans;
  3733. char label[BTRFS_LABEL_SIZE];
  3734. int ret;
  3735. if (!capable(CAP_SYS_ADMIN))
  3736. return -EPERM;
  3737. if (copy_from_user(label, arg, sizeof(label)))
  3738. return -EFAULT;
  3739. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  3740. pr_err("btrfs: unable to set label with more than %d bytes\n",
  3741. BTRFS_LABEL_SIZE - 1);
  3742. return -EINVAL;
  3743. }
  3744. ret = mnt_want_write_file(file);
  3745. if (ret)
  3746. return ret;
  3747. trans = btrfs_start_transaction(root, 0);
  3748. if (IS_ERR(trans)) {
  3749. ret = PTR_ERR(trans);
  3750. goto out_unlock;
  3751. }
  3752. spin_lock(&root->fs_info->super_lock);
  3753. strcpy(super_block->label, label);
  3754. spin_unlock(&root->fs_info->super_lock);
  3755. ret = btrfs_end_transaction(trans, root);
  3756. out_unlock:
  3757. mnt_drop_write_file(file);
  3758. return ret;
  3759. }
  3760. long btrfs_ioctl(struct file *file, unsigned int
  3761. cmd, unsigned long arg)
  3762. {
  3763. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3764. void __user *argp = (void __user *)arg;
  3765. switch (cmd) {
  3766. case FS_IOC_GETFLAGS:
  3767. return btrfs_ioctl_getflags(file, argp);
  3768. case FS_IOC_SETFLAGS:
  3769. return btrfs_ioctl_setflags(file, argp);
  3770. case FS_IOC_GETVERSION:
  3771. return btrfs_ioctl_getversion(file, argp);
  3772. case FITRIM:
  3773. return btrfs_ioctl_fitrim(file, argp);
  3774. case BTRFS_IOC_SNAP_CREATE:
  3775. return btrfs_ioctl_snap_create(file, argp, 0);
  3776. case BTRFS_IOC_SNAP_CREATE_V2:
  3777. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  3778. case BTRFS_IOC_SUBVOL_CREATE:
  3779. return btrfs_ioctl_snap_create(file, argp, 1);
  3780. case BTRFS_IOC_SUBVOL_CREATE_V2:
  3781. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  3782. case BTRFS_IOC_SNAP_DESTROY:
  3783. return btrfs_ioctl_snap_destroy(file, argp);
  3784. case BTRFS_IOC_SUBVOL_GETFLAGS:
  3785. return btrfs_ioctl_subvol_getflags(file, argp);
  3786. case BTRFS_IOC_SUBVOL_SETFLAGS:
  3787. return btrfs_ioctl_subvol_setflags(file, argp);
  3788. case BTRFS_IOC_DEFAULT_SUBVOL:
  3789. return btrfs_ioctl_default_subvol(file, argp);
  3790. case BTRFS_IOC_DEFRAG:
  3791. return btrfs_ioctl_defrag(file, NULL);
  3792. case BTRFS_IOC_DEFRAG_RANGE:
  3793. return btrfs_ioctl_defrag(file, argp);
  3794. case BTRFS_IOC_RESIZE:
  3795. return btrfs_ioctl_resize(file, argp);
  3796. case BTRFS_IOC_ADD_DEV:
  3797. return btrfs_ioctl_add_dev(root, argp);
  3798. case BTRFS_IOC_RM_DEV:
  3799. return btrfs_ioctl_rm_dev(file, argp);
  3800. case BTRFS_IOC_FS_INFO:
  3801. return btrfs_ioctl_fs_info(root, argp);
  3802. case BTRFS_IOC_DEV_INFO:
  3803. return btrfs_ioctl_dev_info(root, argp);
  3804. case BTRFS_IOC_BALANCE:
  3805. return btrfs_ioctl_balance(file, NULL);
  3806. case BTRFS_IOC_CLONE:
  3807. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  3808. case BTRFS_IOC_CLONE_RANGE:
  3809. return btrfs_ioctl_clone_range(file, argp);
  3810. case BTRFS_IOC_TRANS_START:
  3811. return btrfs_ioctl_trans_start(file);
  3812. case BTRFS_IOC_TRANS_END:
  3813. return btrfs_ioctl_trans_end(file);
  3814. case BTRFS_IOC_TREE_SEARCH:
  3815. return btrfs_ioctl_tree_search(file, argp);
  3816. case BTRFS_IOC_INO_LOOKUP:
  3817. return btrfs_ioctl_ino_lookup(file, argp);
  3818. case BTRFS_IOC_INO_PATHS:
  3819. return btrfs_ioctl_ino_to_path(root, argp);
  3820. case BTRFS_IOC_LOGICAL_INO:
  3821. return btrfs_ioctl_logical_to_ino(root, argp);
  3822. case BTRFS_IOC_SPACE_INFO:
  3823. return btrfs_ioctl_space_info(root, argp);
  3824. case BTRFS_IOC_SYNC:
  3825. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  3826. return 0;
  3827. case BTRFS_IOC_START_SYNC:
  3828. return btrfs_ioctl_start_sync(root, argp);
  3829. case BTRFS_IOC_WAIT_SYNC:
  3830. return btrfs_ioctl_wait_sync(root, argp);
  3831. case BTRFS_IOC_SCRUB:
  3832. return btrfs_ioctl_scrub(file, argp);
  3833. case BTRFS_IOC_SCRUB_CANCEL:
  3834. return btrfs_ioctl_scrub_cancel(root, argp);
  3835. case BTRFS_IOC_SCRUB_PROGRESS:
  3836. return btrfs_ioctl_scrub_progress(root, argp);
  3837. case BTRFS_IOC_BALANCE_V2:
  3838. return btrfs_ioctl_balance(file, argp);
  3839. case BTRFS_IOC_BALANCE_CTL:
  3840. return btrfs_ioctl_balance_ctl(root, arg);
  3841. case BTRFS_IOC_BALANCE_PROGRESS:
  3842. return btrfs_ioctl_balance_progress(root, argp);
  3843. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  3844. return btrfs_ioctl_set_received_subvol(file, argp);
  3845. case BTRFS_IOC_SEND:
  3846. return btrfs_ioctl_send(file, argp);
  3847. case BTRFS_IOC_GET_DEV_STATS:
  3848. return btrfs_ioctl_get_dev_stats(root, argp);
  3849. case BTRFS_IOC_QUOTA_CTL:
  3850. return btrfs_ioctl_quota_ctl(file, argp);
  3851. case BTRFS_IOC_QGROUP_ASSIGN:
  3852. return btrfs_ioctl_qgroup_assign(file, argp);
  3853. case BTRFS_IOC_QGROUP_CREATE:
  3854. return btrfs_ioctl_qgroup_create(file, argp);
  3855. case BTRFS_IOC_QGROUP_LIMIT:
  3856. return btrfs_ioctl_qgroup_limit(file, argp);
  3857. case BTRFS_IOC_QUOTA_RESCAN:
  3858. return btrfs_ioctl_quota_rescan(file, argp);
  3859. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  3860. return btrfs_ioctl_quota_rescan_status(file, argp);
  3861. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  3862. return btrfs_ioctl_quota_rescan_wait(file, argp);
  3863. case BTRFS_IOC_DEV_REPLACE:
  3864. return btrfs_ioctl_dev_replace(root, argp);
  3865. case BTRFS_IOC_GET_FSLABEL:
  3866. return btrfs_ioctl_get_fslabel(file, argp);
  3867. case BTRFS_IOC_SET_FSLABEL:
  3868. return btrfs_ioctl_set_fslabel(file, argp);
  3869. case BTRFS_IOC_FILE_EXTENT_SAME:
  3870. return btrfs_ioctl_file_extent_same(file, argp);
  3871. }
  3872. return -ENOTTY;
  3873. }