ioctl.c 99 KB

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