ioctl.c 108 KB

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