ioctl.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248
  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 inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
  2132. {
  2133. /* do any pending delalloc/csum calc on src, one way or
  2134. another, and lock file content */
  2135. while (1) {
  2136. struct btrfs_ordered_extent *ordered;
  2137. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2138. ordered = btrfs_lookup_first_ordered_extent(inode,
  2139. off + len - 1);
  2140. if (!ordered &&
  2141. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2142. off + len - 1, EXTENT_DELALLOC, 0, NULL))
  2143. break;
  2144. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2145. if (ordered)
  2146. btrfs_put_ordered_extent(ordered);
  2147. btrfs_wait_ordered_range(inode, off, len);
  2148. }
  2149. }
  2150. /**
  2151. * btrfs_clone() - clone a range from inode file to another
  2152. *
  2153. * @src: Inode to clone from
  2154. * @inode: Inode to clone to
  2155. * @off: Offset within source to start clone from
  2156. * @olen: Original length, passed by user, of range to clone
  2157. * @olen_aligned: Block-aligned value of olen, extent_same uses
  2158. * identical values here
  2159. * @destoff: Offset within @inode to start clone
  2160. */
  2161. static int btrfs_clone(struct inode *src, struct inode *inode,
  2162. u64 off, u64 olen, u64 olen_aligned, u64 destoff)
  2163. {
  2164. struct btrfs_root *root = BTRFS_I(inode)->root;
  2165. struct btrfs_path *path = NULL;
  2166. struct extent_buffer *leaf;
  2167. struct btrfs_trans_handle *trans;
  2168. char *buf = NULL;
  2169. struct btrfs_key key;
  2170. u32 nritems;
  2171. int slot;
  2172. int ret;
  2173. u64 len = olen_aligned;
  2174. ret = -ENOMEM;
  2175. buf = vmalloc(btrfs_level_size(root, 0));
  2176. if (!buf)
  2177. return ret;
  2178. path = btrfs_alloc_path();
  2179. if (!path) {
  2180. vfree(buf);
  2181. return ret;
  2182. }
  2183. path->reada = 2;
  2184. /* clone data */
  2185. key.objectid = btrfs_ino(src);
  2186. key.type = BTRFS_EXTENT_DATA_KEY;
  2187. key.offset = 0;
  2188. while (1) {
  2189. /*
  2190. * note the key will change type as we walk through the
  2191. * tree.
  2192. */
  2193. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2194. 0, 0);
  2195. if (ret < 0)
  2196. goto out;
  2197. nritems = btrfs_header_nritems(path->nodes[0]);
  2198. if (path->slots[0] >= nritems) {
  2199. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2200. if (ret < 0)
  2201. goto out;
  2202. if (ret > 0)
  2203. break;
  2204. nritems = btrfs_header_nritems(path->nodes[0]);
  2205. }
  2206. leaf = path->nodes[0];
  2207. slot = path->slots[0];
  2208. btrfs_item_key_to_cpu(leaf, &key, slot);
  2209. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2210. key.objectid != btrfs_ino(src))
  2211. break;
  2212. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2213. struct btrfs_file_extent_item *extent;
  2214. int type;
  2215. u32 size;
  2216. struct btrfs_key new_key;
  2217. u64 disko = 0, diskl = 0;
  2218. u64 datao = 0, datal = 0;
  2219. u8 comp;
  2220. u64 endoff;
  2221. size = btrfs_item_size_nr(leaf, slot);
  2222. read_extent_buffer(leaf, buf,
  2223. btrfs_item_ptr_offset(leaf, slot),
  2224. size);
  2225. extent = btrfs_item_ptr(leaf, slot,
  2226. struct btrfs_file_extent_item);
  2227. comp = btrfs_file_extent_compression(leaf, extent);
  2228. type = btrfs_file_extent_type(leaf, extent);
  2229. if (type == BTRFS_FILE_EXTENT_REG ||
  2230. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2231. disko = btrfs_file_extent_disk_bytenr(leaf,
  2232. extent);
  2233. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2234. extent);
  2235. datao = btrfs_file_extent_offset(leaf, extent);
  2236. datal = btrfs_file_extent_num_bytes(leaf,
  2237. extent);
  2238. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2239. /* take upper bound, may be compressed */
  2240. datal = btrfs_file_extent_ram_bytes(leaf,
  2241. extent);
  2242. }
  2243. btrfs_release_path(path);
  2244. if (key.offset + datal <= off ||
  2245. key.offset >= off + len - 1)
  2246. goto next;
  2247. memcpy(&new_key, &key, sizeof(new_key));
  2248. new_key.objectid = btrfs_ino(inode);
  2249. if (off <= key.offset)
  2250. new_key.offset = key.offset + destoff - off;
  2251. else
  2252. new_key.offset = destoff;
  2253. /*
  2254. * 1 - adjusting old extent (we may have to split it)
  2255. * 1 - add new extent
  2256. * 1 - inode update
  2257. */
  2258. trans = btrfs_start_transaction(root, 3);
  2259. if (IS_ERR(trans)) {
  2260. ret = PTR_ERR(trans);
  2261. goto out;
  2262. }
  2263. if (type == BTRFS_FILE_EXTENT_REG ||
  2264. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2265. /*
  2266. * a | --- range to clone ---| b
  2267. * | ------------- extent ------------- |
  2268. */
  2269. /* substract range b */
  2270. if (key.offset + datal > off + len)
  2271. datal = off + len - key.offset;
  2272. /* substract range a */
  2273. if (off > key.offset) {
  2274. datao += off - key.offset;
  2275. datal -= off - key.offset;
  2276. }
  2277. ret = btrfs_drop_extents(trans, root, inode,
  2278. new_key.offset,
  2279. new_key.offset + datal,
  2280. 1);
  2281. if (ret) {
  2282. btrfs_abort_transaction(trans, root,
  2283. ret);
  2284. btrfs_end_transaction(trans, root);
  2285. goto out;
  2286. }
  2287. ret = btrfs_insert_empty_item(trans, root, path,
  2288. &new_key, size);
  2289. if (ret) {
  2290. btrfs_abort_transaction(trans, root,
  2291. ret);
  2292. btrfs_end_transaction(trans, root);
  2293. goto out;
  2294. }
  2295. leaf = path->nodes[0];
  2296. slot = path->slots[0];
  2297. write_extent_buffer(leaf, buf,
  2298. btrfs_item_ptr_offset(leaf, slot),
  2299. size);
  2300. extent = btrfs_item_ptr(leaf, slot,
  2301. struct btrfs_file_extent_item);
  2302. /* disko == 0 means it's a hole */
  2303. if (!disko)
  2304. datao = 0;
  2305. btrfs_set_file_extent_offset(leaf, extent,
  2306. datao);
  2307. btrfs_set_file_extent_num_bytes(leaf, extent,
  2308. datal);
  2309. if (disko) {
  2310. inode_add_bytes(inode, datal);
  2311. ret = btrfs_inc_extent_ref(trans, root,
  2312. disko, diskl, 0,
  2313. root->root_key.objectid,
  2314. btrfs_ino(inode),
  2315. new_key.offset - datao,
  2316. 0);
  2317. if (ret) {
  2318. btrfs_abort_transaction(trans,
  2319. root,
  2320. ret);
  2321. btrfs_end_transaction(trans,
  2322. root);
  2323. goto out;
  2324. }
  2325. }
  2326. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2327. u64 skip = 0;
  2328. u64 trim = 0;
  2329. if (off > key.offset) {
  2330. skip = off - key.offset;
  2331. new_key.offset += skip;
  2332. }
  2333. if (key.offset + datal > off + len)
  2334. trim = key.offset + datal - (off + len);
  2335. if (comp && (skip || trim)) {
  2336. ret = -EINVAL;
  2337. btrfs_end_transaction(trans, root);
  2338. goto out;
  2339. }
  2340. size -= skip + trim;
  2341. datal -= skip + trim;
  2342. ret = btrfs_drop_extents(trans, root, inode,
  2343. new_key.offset,
  2344. new_key.offset + datal,
  2345. 1);
  2346. if (ret) {
  2347. btrfs_abort_transaction(trans, root,
  2348. ret);
  2349. btrfs_end_transaction(trans, root);
  2350. goto out;
  2351. }
  2352. ret = btrfs_insert_empty_item(trans, root, path,
  2353. &new_key, size);
  2354. if (ret) {
  2355. btrfs_abort_transaction(trans, root,
  2356. ret);
  2357. btrfs_end_transaction(trans, root);
  2358. goto out;
  2359. }
  2360. if (skip) {
  2361. u32 start =
  2362. btrfs_file_extent_calc_inline_size(0);
  2363. memmove(buf+start, buf+start+skip,
  2364. datal);
  2365. }
  2366. leaf = path->nodes[0];
  2367. slot = path->slots[0];
  2368. write_extent_buffer(leaf, buf,
  2369. btrfs_item_ptr_offset(leaf, slot),
  2370. size);
  2371. inode_add_bytes(inode, datal);
  2372. }
  2373. btrfs_mark_buffer_dirty(leaf);
  2374. btrfs_release_path(path);
  2375. inode_inc_iversion(inode);
  2376. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2377. /*
  2378. * we round up to the block size at eof when
  2379. * determining which extents to clone above,
  2380. * but shouldn't round up the file size
  2381. */
  2382. endoff = new_key.offset + datal;
  2383. if (endoff > destoff+olen)
  2384. endoff = destoff+olen;
  2385. if (endoff > inode->i_size)
  2386. btrfs_i_size_write(inode, endoff);
  2387. ret = btrfs_update_inode(trans, root, inode);
  2388. if (ret) {
  2389. btrfs_abort_transaction(trans, root, ret);
  2390. btrfs_end_transaction(trans, root);
  2391. goto out;
  2392. }
  2393. ret = btrfs_end_transaction(trans, root);
  2394. }
  2395. next:
  2396. btrfs_release_path(path);
  2397. key.offset++;
  2398. }
  2399. ret = 0;
  2400. out:
  2401. btrfs_release_path(path);
  2402. btrfs_free_path(path);
  2403. vfree(buf);
  2404. return ret;
  2405. }
  2406. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2407. u64 off, u64 olen, u64 destoff)
  2408. {
  2409. struct inode *inode = fdentry(file)->d_inode;
  2410. struct btrfs_root *root = BTRFS_I(inode)->root;
  2411. struct fd src_file;
  2412. struct inode *src;
  2413. int ret;
  2414. u64 len = olen;
  2415. u64 bs = root->fs_info->sb->s_blocksize;
  2416. int same_inode = 0;
  2417. /*
  2418. * TODO:
  2419. * - split compressed inline extents. annoying: we need to
  2420. * decompress into destination's address_space (the file offset
  2421. * may change, so source mapping won't do), then recompress (or
  2422. * otherwise reinsert) a subrange.
  2423. * - allow ranges within the same file to be cloned (provided
  2424. * they don't overlap)?
  2425. */
  2426. /* the destination must be opened for writing */
  2427. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  2428. return -EINVAL;
  2429. if (btrfs_root_readonly(root))
  2430. return -EROFS;
  2431. ret = mnt_want_write_file(file);
  2432. if (ret)
  2433. return ret;
  2434. src_file = fdget(srcfd);
  2435. if (!src_file.file) {
  2436. ret = -EBADF;
  2437. goto out_drop_write;
  2438. }
  2439. ret = -EXDEV;
  2440. if (src_file.file->f_path.mnt != file->f_path.mnt)
  2441. goto out_fput;
  2442. src = file_inode(src_file.file);
  2443. ret = -EINVAL;
  2444. if (src == inode)
  2445. same_inode = 1;
  2446. /* the src must be open for reading */
  2447. if (!(src_file.file->f_mode & FMODE_READ))
  2448. goto out_fput;
  2449. /* don't make the dst file partly checksummed */
  2450. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2451. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  2452. goto out_fput;
  2453. ret = -EISDIR;
  2454. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  2455. goto out_fput;
  2456. ret = -EXDEV;
  2457. if (src->i_sb != inode->i_sb)
  2458. goto out_fput;
  2459. if (!same_inode) {
  2460. if (inode < src) {
  2461. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2462. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2463. } else {
  2464. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2465. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2466. }
  2467. } else {
  2468. mutex_lock(&src->i_mutex);
  2469. }
  2470. /* determine range to clone */
  2471. ret = -EINVAL;
  2472. if (off + len > src->i_size || off + len < off)
  2473. goto out_unlock;
  2474. if (len == 0)
  2475. olen = len = src->i_size - off;
  2476. /* if we extend to eof, continue to block boundary */
  2477. if (off + len == src->i_size)
  2478. len = ALIGN(src->i_size, bs) - off;
  2479. /* verify the end result is block aligned */
  2480. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2481. !IS_ALIGNED(destoff, bs))
  2482. goto out_unlock;
  2483. /* verify if ranges are overlapped within the same file */
  2484. if (same_inode) {
  2485. if (destoff + len > off && destoff < off + len)
  2486. goto out_unlock;
  2487. }
  2488. if (destoff > inode->i_size) {
  2489. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2490. if (ret)
  2491. goto out_unlock;
  2492. }
  2493. /* truncate page cache pages from target inode range */
  2494. truncate_inode_pages_range(&inode->i_data, destoff,
  2495. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2496. lock_extent_range(src, off, len);
  2497. ret = btrfs_clone(src, inode, off, olen, len, destoff);
  2498. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2499. out_unlock:
  2500. mutex_unlock(&src->i_mutex);
  2501. if (!same_inode)
  2502. mutex_unlock(&inode->i_mutex);
  2503. out_fput:
  2504. fdput(src_file);
  2505. out_drop_write:
  2506. mnt_drop_write_file(file);
  2507. return ret;
  2508. }
  2509. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2510. {
  2511. struct btrfs_ioctl_clone_range_args args;
  2512. if (copy_from_user(&args, argp, sizeof(args)))
  2513. return -EFAULT;
  2514. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2515. args.src_length, args.dest_offset);
  2516. }
  2517. /*
  2518. * there are many ways the trans_start and trans_end ioctls can lead
  2519. * to deadlocks. They should only be used by applications that
  2520. * basically own the machine, and have a very in depth understanding
  2521. * of all the possible deadlocks and enospc problems.
  2522. */
  2523. static long btrfs_ioctl_trans_start(struct file *file)
  2524. {
  2525. struct inode *inode = file_inode(file);
  2526. struct btrfs_root *root = BTRFS_I(inode)->root;
  2527. struct btrfs_trans_handle *trans;
  2528. int ret;
  2529. ret = -EPERM;
  2530. if (!capable(CAP_SYS_ADMIN))
  2531. goto out;
  2532. ret = -EINPROGRESS;
  2533. if (file->private_data)
  2534. goto out;
  2535. ret = -EROFS;
  2536. if (btrfs_root_readonly(root))
  2537. goto out;
  2538. ret = mnt_want_write_file(file);
  2539. if (ret)
  2540. goto out;
  2541. atomic_inc(&root->fs_info->open_ioctl_trans);
  2542. ret = -ENOMEM;
  2543. trans = btrfs_start_ioctl_transaction(root);
  2544. if (IS_ERR(trans))
  2545. goto out_drop;
  2546. file->private_data = trans;
  2547. return 0;
  2548. out_drop:
  2549. atomic_dec(&root->fs_info->open_ioctl_trans);
  2550. mnt_drop_write_file(file);
  2551. out:
  2552. return ret;
  2553. }
  2554. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2555. {
  2556. struct inode *inode = file_inode(file);
  2557. struct btrfs_root *root = BTRFS_I(inode)->root;
  2558. struct btrfs_root *new_root;
  2559. struct btrfs_dir_item *di;
  2560. struct btrfs_trans_handle *trans;
  2561. struct btrfs_path *path;
  2562. struct btrfs_key location;
  2563. struct btrfs_disk_key disk_key;
  2564. u64 objectid = 0;
  2565. u64 dir_id;
  2566. int ret;
  2567. if (!capable(CAP_SYS_ADMIN))
  2568. return -EPERM;
  2569. ret = mnt_want_write_file(file);
  2570. if (ret)
  2571. return ret;
  2572. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2573. ret = -EFAULT;
  2574. goto out;
  2575. }
  2576. if (!objectid)
  2577. objectid = root->root_key.objectid;
  2578. location.objectid = objectid;
  2579. location.type = BTRFS_ROOT_ITEM_KEY;
  2580. location.offset = (u64)-1;
  2581. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2582. if (IS_ERR(new_root)) {
  2583. ret = PTR_ERR(new_root);
  2584. goto out;
  2585. }
  2586. path = btrfs_alloc_path();
  2587. if (!path) {
  2588. ret = -ENOMEM;
  2589. goto out;
  2590. }
  2591. path->leave_spinning = 1;
  2592. trans = btrfs_start_transaction(root, 1);
  2593. if (IS_ERR(trans)) {
  2594. btrfs_free_path(path);
  2595. ret = PTR_ERR(trans);
  2596. goto out;
  2597. }
  2598. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2599. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2600. dir_id, "default", 7, 1);
  2601. if (IS_ERR_OR_NULL(di)) {
  2602. btrfs_free_path(path);
  2603. btrfs_end_transaction(trans, root);
  2604. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2605. "this isn't going to work\n");
  2606. ret = -ENOENT;
  2607. goto out;
  2608. }
  2609. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2610. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2611. btrfs_mark_buffer_dirty(path->nodes[0]);
  2612. btrfs_free_path(path);
  2613. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2614. btrfs_end_transaction(trans, root);
  2615. out:
  2616. mnt_drop_write_file(file);
  2617. return ret;
  2618. }
  2619. void btrfs_get_block_group_info(struct list_head *groups_list,
  2620. struct btrfs_ioctl_space_info *space)
  2621. {
  2622. struct btrfs_block_group_cache *block_group;
  2623. space->total_bytes = 0;
  2624. space->used_bytes = 0;
  2625. space->flags = 0;
  2626. list_for_each_entry(block_group, groups_list, list) {
  2627. space->flags = block_group->flags;
  2628. space->total_bytes += block_group->key.offset;
  2629. space->used_bytes +=
  2630. btrfs_block_group_used(&block_group->item);
  2631. }
  2632. }
  2633. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2634. {
  2635. struct btrfs_ioctl_space_args space_args;
  2636. struct btrfs_ioctl_space_info space;
  2637. struct btrfs_ioctl_space_info *dest;
  2638. struct btrfs_ioctl_space_info *dest_orig;
  2639. struct btrfs_ioctl_space_info __user *user_dest;
  2640. struct btrfs_space_info *info;
  2641. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2642. BTRFS_BLOCK_GROUP_SYSTEM,
  2643. BTRFS_BLOCK_GROUP_METADATA,
  2644. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2645. int num_types = 4;
  2646. int alloc_size;
  2647. int ret = 0;
  2648. u64 slot_count = 0;
  2649. int i, c;
  2650. if (copy_from_user(&space_args,
  2651. (struct btrfs_ioctl_space_args __user *)arg,
  2652. sizeof(space_args)))
  2653. return -EFAULT;
  2654. for (i = 0; i < num_types; i++) {
  2655. struct btrfs_space_info *tmp;
  2656. info = NULL;
  2657. rcu_read_lock();
  2658. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2659. list) {
  2660. if (tmp->flags == types[i]) {
  2661. info = tmp;
  2662. break;
  2663. }
  2664. }
  2665. rcu_read_unlock();
  2666. if (!info)
  2667. continue;
  2668. down_read(&info->groups_sem);
  2669. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2670. if (!list_empty(&info->block_groups[c]))
  2671. slot_count++;
  2672. }
  2673. up_read(&info->groups_sem);
  2674. }
  2675. /* space_slots == 0 means they are asking for a count */
  2676. if (space_args.space_slots == 0) {
  2677. space_args.total_spaces = slot_count;
  2678. goto out;
  2679. }
  2680. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2681. alloc_size = sizeof(*dest) * slot_count;
  2682. /* we generally have at most 6 or so space infos, one for each raid
  2683. * level. So, a whole page should be more than enough for everyone
  2684. */
  2685. if (alloc_size > PAGE_CACHE_SIZE)
  2686. return -ENOMEM;
  2687. space_args.total_spaces = 0;
  2688. dest = kmalloc(alloc_size, GFP_NOFS);
  2689. if (!dest)
  2690. return -ENOMEM;
  2691. dest_orig = dest;
  2692. /* now we have a buffer to copy into */
  2693. for (i = 0; i < num_types; i++) {
  2694. struct btrfs_space_info *tmp;
  2695. if (!slot_count)
  2696. break;
  2697. info = NULL;
  2698. rcu_read_lock();
  2699. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2700. list) {
  2701. if (tmp->flags == types[i]) {
  2702. info = tmp;
  2703. break;
  2704. }
  2705. }
  2706. rcu_read_unlock();
  2707. if (!info)
  2708. continue;
  2709. down_read(&info->groups_sem);
  2710. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2711. if (!list_empty(&info->block_groups[c])) {
  2712. btrfs_get_block_group_info(
  2713. &info->block_groups[c], &space);
  2714. memcpy(dest, &space, sizeof(space));
  2715. dest++;
  2716. space_args.total_spaces++;
  2717. slot_count--;
  2718. }
  2719. if (!slot_count)
  2720. break;
  2721. }
  2722. up_read(&info->groups_sem);
  2723. }
  2724. user_dest = (struct btrfs_ioctl_space_info __user *)
  2725. (arg + sizeof(struct btrfs_ioctl_space_args));
  2726. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2727. ret = -EFAULT;
  2728. kfree(dest_orig);
  2729. out:
  2730. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2731. ret = -EFAULT;
  2732. return ret;
  2733. }
  2734. /*
  2735. * there are many ways the trans_start and trans_end ioctls can lead
  2736. * to deadlocks. They should only be used by applications that
  2737. * basically own the machine, and have a very in depth understanding
  2738. * of all the possible deadlocks and enospc problems.
  2739. */
  2740. long btrfs_ioctl_trans_end(struct file *file)
  2741. {
  2742. struct inode *inode = file_inode(file);
  2743. struct btrfs_root *root = BTRFS_I(inode)->root;
  2744. struct btrfs_trans_handle *trans;
  2745. trans = file->private_data;
  2746. if (!trans)
  2747. return -EINVAL;
  2748. file->private_data = NULL;
  2749. btrfs_end_transaction(trans, root);
  2750. atomic_dec(&root->fs_info->open_ioctl_trans);
  2751. mnt_drop_write_file(file);
  2752. return 0;
  2753. }
  2754. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  2755. void __user *argp)
  2756. {
  2757. struct btrfs_trans_handle *trans;
  2758. u64 transid;
  2759. int ret;
  2760. trans = btrfs_attach_transaction_barrier(root);
  2761. if (IS_ERR(trans)) {
  2762. if (PTR_ERR(trans) != -ENOENT)
  2763. return PTR_ERR(trans);
  2764. /* No running transaction, don't bother */
  2765. transid = root->fs_info->last_trans_committed;
  2766. goto out;
  2767. }
  2768. transid = trans->transid;
  2769. ret = btrfs_commit_transaction_async(trans, root, 0);
  2770. if (ret) {
  2771. btrfs_end_transaction(trans, root);
  2772. return ret;
  2773. }
  2774. out:
  2775. if (argp)
  2776. if (copy_to_user(argp, &transid, sizeof(transid)))
  2777. return -EFAULT;
  2778. return 0;
  2779. }
  2780. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  2781. void __user *argp)
  2782. {
  2783. u64 transid;
  2784. if (argp) {
  2785. if (copy_from_user(&transid, argp, sizeof(transid)))
  2786. return -EFAULT;
  2787. } else {
  2788. transid = 0; /* current trans */
  2789. }
  2790. return btrfs_wait_for_commit(root, transid);
  2791. }
  2792. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  2793. {
  2794. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2795. struct btrfs_ioctl_scrub_args *sa;
  2796. int ret;
  2797. if (!capable(CAP_SYS_ADMIN))
  2798. return -EPERM;
  2799. sa = memdup_user(arg, sizeof(*sa));
  2800. if (IS_ERR(sa))
  2801. return PTR_ERR(sa);
  2802. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  2803. ret = mnt_want_write_file(file);
  2804. if (ret)
  2805. goto out;
  2806. }
  2807. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  2808. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  2809. 0);
  2810. if (copy_to_user(arg, sa, sizeof(*sa)))
  2811. ret = -EFAULT;
  2812. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  2813. mnt_drop_write_file(file);
  2814. out:
  2815. kfree(sa);
  2816. return ret;
  2817. }
  2818. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2819. {
  2820. if (!capable(CAP_SYS_ADMIN))
  2821. return -EPERM;
  2822. return btrfs_scrub_cancel(root->fs_info);
  2823. }
  2824. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2825. void __user *arg)
  2826. {
  2827. struct btrfs_ioctl_scrub_args *sa;
  2828. int ret;
  2829. if (!capable(CAP_SYS_ADMIN))
  2830. return -EPERM;
  2831. sa = memdup_user(arg, sizeof(*sa));
  2832. if (IS_ERR(sa))
  2833. return PTR_ERR(sa);
  2834. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2835. if (copy_to_user(arg, sa, sizeof(*sa)))
  2836. ret = -EFAULT;
  2837. kfree(sa);
  2838. return ret;
  2839. }
  2840. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  2841. void __user *arg)
  2842. {
  2843. struct btrfs_ioctl_get_dev_stats *sa;
  2844. int ret;
  2845. sa = memdup_user(arg, sizeof(*sa));
  2846. if (IS_ERR(sa))
  2847. return PTR_ERR(sa);
  2848. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  2849. kfree(sa);
  2850. return -EPERM;
  2851. }
  2852. ret = btrfs_get_dev_stats(root, sa);
  2853. if (copy_to_user(arg, sa, sizeof(*sa)))
  2854. ret = -EFAULT;
  2855. kfree(sa);
  2856. return ret;
  2857. }
  2858. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  2859. {
  2860. struct btrfs_ioctl_dev_replace_args *p;
  2861. int ret;
  2862. if (!capable(CAP_SYS_ADMIN))
  2863. return -EPERM;
  2864. p = memdup_user(arg, sizeof(*p));
  2865. if (IS_ERR(p))
  2866. return PTR_ERR(p);
  2867. switch (p->cmd) {
  2868. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  2869. if (atomic_xchg(
  2870. &root->fs_info->mutually_exclusive_operation_running,
  2871. 1)) {
  2872. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2873. ret = -EINPROGRESS;
  2874. } else {
  2875. ret = btrfs_dev_replace_start(root, p);
  2876. atomic_set(
  2877. &root->fs_info->mutually_exclusive_operation_running,
  2878. 0);
  2879. }
  2880. break;
  2881. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  2882. btrfs_dev_replace_status(root->fs_info, p);
  2883. ret = 0;
  2884. break;
  2885. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  2886. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  2887. break;
  2888. default:
  2889. ret = -EINVAL;
  2890. break;
  2891. }
  2892. if (copy_to_user(arg, p, sizeof(*p)))
  2893. ret = -EFAULT;
  2894. kfree(p);
  2895. return ret;
  2896. }
  2897. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  2898. {
  2899. int ret = 0;
  2900. int i;
  2901. u64 rel_ptr;
  2902. int size;
  2903. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  2904. struct inode_fs_paths *ipath = NULL;
  2905. struct btrfs_path *path;
  2906. if (!capable(CAP_DAC_READ_SEARCH))
  2907. return -EPERM;
  2908. path = btrfs_alloc_path();
  2909. if (!path) {
  2910. ret = -ENOMEM;
  2911. goto out;
  2912. }
  2913. ipa = memdup_user(arg, sizeof(*ipa));
  2914. if (IS_ERR(ipa)) {
  2915. ret = PTR_ERR(ipa);
  2916. ipa = NULL;
  2917. goto out;
  2918. }
  2919. size = min_t(u32, ipa->size, 4096);
  2920. ipath = init_ipath(size, root, path);
  2921. if (IS_ERR(ipath)) {
  2922. ret = PTR_ERR(ipath);
  2923. ipath = NULL;
  2924. goto out;
  2925. }
  2926. ret = paths_from_inode(ipa->inum, ipath);
  2927. if (ret < 0)
  2928. goto out;
  2929. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  2930. rel_ptr = ipath->fspath->val[i] -
  2931. (u64)(unsigned long)ipath->fspath->val;
  2932. ipath->fspath->val[i] = rel_ptr;
  2933. }
  2934. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  2935. (void *)(unsigned long)ipath->fspath, size);
  2936. if (ret) {
  2937. ret = -EFAULT;
  2938. goto out;
  2939. }
  2940. out:
  2941. btrfs_free_path(path);
  2942. free_ipath(ipath);
  2943. kfree(ipa);
  2944. return ret;
  2945. }
  2946. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  2947. {
  2948. struct btrfs_data_container *inodes = ctx;
  2949. const size_t c = 3 * sizeof(u64);
  2950. if (inodes->bytes_left >= c) {
  2951. inodes->bytes_left -= c;
  2952. inodes->val[inodes->elem_cnt] = inum;
  2953. inodes->val[inodes->elem_cnt + 1] = offset;
  2954. inodes->val[inodes->elem_cnt + 2] = root;
  2955. inodes->elem_cnt += 3;
  2956. } else {
  2957. inodes->bytes_missing += c - inodes->bytes_left;
  2958. inodes->bytes_left = 0;
  2959. inodes->elem_missed += 3;
  2960. }
  2961. return 0;
  2962. }
  2963. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  2964. void __user *arg)
  2965. {
  2966. int ret = 0;
  2967. int size;
  2968. struct btrfs_ioctl_logical_ino_args *loi;
  2969. struct btrfs_data_container *inodes = NULL;
  2970. struct btrfs_path *path = NULL;
  2971. if (!capable(CAP_SYS_ADMIN))
  2972. return -EPERM;
  2973. loi = memdup_user(arg, sizeof(*loi));
  2974. if (IS_ERR(loi)) {
  2975. ret = PTR_ERR(loi);
  2976. loi = NULL;
  2977. goto out;
  2978. }
  2979. path = btrfs_alloc_path();
  2980. if (!path) {
  2981. ret = -ENOMEM;
  2982. goto out;
  2983. }
  2984. size = min_t(u32, loi->size, 64 * 1024);
  2985. inodes = init_data_container(size);
  2986. if (IS_ERR(inodes)) {
  2987. ret = PTR_ERR(inodes);
  2988. inodes = NULL;
  2989. goto out;
  2990. }
  2991. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  2992. build_ino_list, inodes);
  2993. if (ret == -EINVAL)
  2994. ret = -ENOENT;
  2995. if (ret < 0)
  2996. goto out;
  2997. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  2998. (void *)(unsigned long)inodes, size);
  2999. if (ret)
  3000. ret = -EFAULT;
  3001. out:
  3002. btrfs_free_path(path);
  3003. vfree(inodes);
  3004. kfree(loi);
  3005. return ret;
  3006. }
  3007. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  3008. struct btrfs_ioctl_balance_args *bargs)
  3009. {
  3010. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3011. bargs->flags = bctl->flags;
  3012. if (atomic_read(&fs_info->balance_running))
  3013. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  3014. if (atomic_read(&fs_info->balance_pause_req))
  3015. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  3016. if (atomic_read(&fs_info->balance_cancel_req))
  3017. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  3018. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  3019. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  3020. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  3021. if (lock) {
  3022. spin_lock(&fs_info->balance_lock);
  3023. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3024. spin_unlock(&fs_info->balance_lock);
  3025. } else {
  3026. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3027. }
  3028. }
  3029. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  3030. {
  3031. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3032. struct btrfs_fs_info *fs_info = root->fs_info;
  3033. struct btrfs_ioctl_balance_args *bargs;
  3034. struct btrfs_balance_control *bctl;
  3035. bool need_unlock; /* for mut. excl. ops lock */
  3036. int ret;
  3037. if (!capable(CAP_SYS_ADMIN))
  3038. return -EPERM;
  3039. ret = mnt_want_write_file(file);
  3040. if (ret)
  3041. return ret;
  3042. again:
  3043. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  3044. mutex_lock(&fs_info->volume_mutex);
  3045. mutex_lock(&fs_info->balance_mutex);
  3046. need_unlock = true;
  3047. goto locked;
  3048. }
  3049. /*
  3050. * mut. excl. ops lock is locked. Three possibilites:
  3051. * (1) some other op is running
  3052. * (2) balance is running
  3053. * (3) balance is paused -- special case (think resume)
  3054. */
  3055. mutex_lock(&fs_info->balance_mutex);
  3056. if (fs_info->balance_ctl) {
  3057. /* this is either (2) or (3) */
  3058. if (!atomic_read(&fs_info->balance_running)) {
  3059. mutex_unlock(&fs_info->balance_mutex);
  3060. if (!mutex_trylock(&fs_info->volume_mutex))
  3061. goto again;
  3062. mutex_lock(&fs_info->balance_mutex);
  3063. if (fs_info->balance_ctl &&
  3064. !atomic_read(&fs_info->balance_running)) {
  3065. /* this is (3) */
  3066. need_unlock = false;
  3067. goto locked;
  3068. }
  3069. mutex_unlock(&fs_info->balance_mutex);
  3070. mutex_unlock(&fs_info->volume_mutex);
  3071. goto again;
  3072. } else {
  3073. /* this is (2) */
  3074. mutex_unlock(&fs_info->balance_mutex);
  3075. ret = -EINPROGRESS;
  3076. goto out;
  3077. }
  3078. } else {
  3079. /* this is (1) */
  3080. mutex_unlock(&fs_info->balance_mutex);
  3081. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  3082. ret = -EINVAL;
  3083. goto out;
  3084. }
  3085. locked:
  3086. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3087. if (arg) {
  3088. bargs = memdup_user(arg, sizeof(*bargs));
  3089. if (IS_ERR(bargs)) {
  3090. ret = PTR_ERR(bargs);
  3091. goto out_unlock;
  3092. }
  3093. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3094. if (!fs_info->balance_ctl) {
  3095. ret = -ENOTCONN;
  3096. goto out_bargs;
  3097. }
  3098. bctl = fs_info->balance_ctl;
  3099. spin_lock(&fs_info->balance_lock);
  3100. bctl->flags |= BTRFS_BALANCE_RESUME;
  3101. spin_unlock(&fs_info->balance_lock);
  3102. goto do_balance;
  3103. }
  3104. } else {
  3105. bargs = NULL;
  3106. }
  3107. if (fs_info->balance_ctl) {
  3108. ret = -EINPROGRESS;
  3109. goto out_bargs;
  3110. }
  3111. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3112. if (!bctl) {
  3113. ret = -ENOMEM;
  3114. goto out_bargs;
  3115. }
  3116. bctl->fs_info = fs_info;
  3117. if (arg) {
  3118. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3119. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3120. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3121. bctl->flags = bargs->flags;
  3122. } else {
  3123. /* balance everything - no filters */
  3124. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3125. }
  3126. do_balance:
  3127. /*
  3128. * Ownership of bctl and mutually_exclusive_operation_running
  3129. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3130. * or, if restriper was paused all the way until unmount, in
  3131. * free_fs_info. mutually_exclusive_operation_running is
  3132. * cleared in __cancel_balance.
  3133. */
  3134. need_unlock = false;
  3135. ret = btrfs_balance(bctl, bargs);
  3136. if (arg) {
  3137. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3138. ret = -EFAULT;
  3139. }
  3140. out_bargs:
  3141. kfree(bargs);
  3142. out_unlock:
  3143. mutex_unlock(&fs_info->balance_mutex);
  3144. mutex_unlock(&fs_info->volume_mutex);
  3145. if (need_unlock)
  3146. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3147. out:
  3148. mnt_drop_write_file(file);
  3149. return ret;
  3150. }
  3151. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3152. {
  3153. if (!capable(CAP_SYS_ADMIN))
  3154. return -EPERM;
  3155. switch (cmd) {
  3156. case BTRFS_BALANCE_CTL_PAUSE:
  3157. return btrfs_pause_balance(root->fs_info);
  3158. case BTRFS_BALANCE_CTL_CANCEL:
  3159. return btrfs_cancel_balance(root->fs_info);
  3160. }
  3161. return -EINVAL;
  3162. }
  3163. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3164. void __user *arg)
  3165. {
  3166. struct btrfs_fs_info *fs_info = root->fs_info;
  3167. struct btrfs_ioctl_balance_args *bargs;
  3168. int ret = 0;
  3169. if (!capable(CAP_SYS_ADMIN))
  3170. return -EPERM;
  3171. mutex_lock(&fs_info->balance_mutex);
  3172. if (!fs_info->balance_ctl) {
  3173. ret = -ENOTCONN;
  3174. goto out;
  3175. }
  3176. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3177. if (!bargs) {
  3178. ret = -ENOMEM;
  3179. goto out;
  3180. }
  3181. update_ioctl_balance_args(fs_info, 1, bargs);
  3182. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3183. ret = -EFAULT;
  3184. kfree(bargs);
  3185. out:
  3186. mutex_unlock(&fs_info->balance_mutex);
  3187. return ret;
  3188. }
  3189. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3190. {
  3191. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3192. struct btrfs_ioctl_quota_ctl_args *sa;
  3193. struct btrfs_trans_handle *trans = NULL;
  3194. int ret;
  3195. int err;
  3196. if (!capable(CAP_SYS_ADMIN))
  3197. return -EPERM;
  3198. ret = mnt_want_write_file(file);
  3199. if (ret)
  3200. return ret;
  3201. sa = memdup_user(arg, sizeof(*sa));
  3202. if (IS_ERR(sa)) {
  3203. ret = PTR_ERR(sa);
  3204. goto drop_write;
  3205. }
  3206. down_write(&root->fs_info->subvol_sem);
  3207. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  3208. if (IS_ERR(trans)) {
  3209. ret = PTR_ERR(trans);
  3210. goto out;
  3211. }
  3212. switch (sa->cmd) {
  3213. case BTRFS_QUOTA_CTL_ENABLE:
  3214. ret = btrfs_quota_enable(trans, root->fs_info);
  3215. break;
  3216. case BTRFS_QUOTA_CTL_DISABLE:
  3217. ret = btrfs_quota_disable(trans, root->fs_info);
  3218. break;
  3219. default:
  3220. ret = -EINVAL;
  3221. break;
  3222. }
  3223. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  3224. if (err && !ret)
  3225. ret = err;
  3226. out:
  3227. kfree(sa);
  3228. up_write(&root->fs_info->subvol_sem);
  3229. drop_write:
  3230. mnt_drop_write_file(file);
  3231. return ret;
  3232. }
  3233. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3234. {
  3235. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3236. struct btrfs_ioctl_qgroup_assign_args *sa;
  3237. struct btrfs_trans_handle *trans;
  3238. int ret;
  3239. int err;
  3240. if (!capable(CAP_SYS_ADMIN))
  3241. return -EPERM;
  3242. ret = mnt_want_write_file(file);
  3243. if (ret)
  3244. return ret;
  3245. sa = memdup_user(arg, sizeof(*sa));
  3246. if (IS_ERR(sa)) {
  3247. ret = PTR_ERR(sa);
  3248. goto drop_write;
  3249. }
  3250. trans = btrfs_join_transaction(root);
  3251. if (IS_ERR(trans)) {
  3252. ret = PTR_ERR(trans);
  3253. goto out;
  3254. }
  3255. /* FIXME: check if the IDs really exist */
  3256. if (sa->assign) {
  3257. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3258. sa->src, sa->dst);
  3259. } else {
  3260. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3261. sa->src, sa->dst);
  3262. }
  3263. err = btrfs_end_transaction(trans, root);
  3264. if (err && !ret)
  3265. ret = err;
  3266. out:
  3267. kfree(sa);
  3268. drop_write:
  3269. mnt_drop_write_file(file);
  3270. return ret;
  3271. }
  3272. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3273. {
  3274. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3275. struct btrfs_ioctl_qgroup_create_args *sa;
  3276. struct btrfs_trans_handle *trans;
  3277. int ret;
  3278. int err;
  3279. if (!capable(CAP_SYS_ADMIN))
  3280. return -EPERM;
  3281. ret = mnt_want_write_file(file);
  3282. if (ret)
  3283. return ret;
  3284. sa = memdup_user(arg, sizeof(*sa));
  3285. if (IS_ERR(sa)) {
  3286. ret = PTR_ERR(sa);
  3287. goto drop_write;
  3288. }
  3289. if (!sa->qgroupid) {
  3290. ret = -EINVAL;
  3291. goto out;
  3292. }
  3293. trans = btrfs_join_transaction(root);
  3294. if (IS_ERR(trans)) {
  3295. ret = PTR_ERR(trans);
  3296. goto out;
  3297. }
  3298. /* FIXME: check if the IDs really exist */
  3299. if (sa->create) {
  3300. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3301. NULL);
  3302. } else {
  3303. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3304. }
  3305. err = btrfs_end_transaction(trans, root);
  3306. if (err && !ret)
  3307. ret = err;
  3308. out:
  3309. kfree(sa);
  3310. drop_write:
  3311. mnt_drop_write_file(file);
  3312. return ret;
  3313. }
  3314. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3315. {
  3316. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3317. struct btrfs_ioctl_qgroup_limit_args *sa;
  3318. struct btrfs_trans_handle *trans;
  3319. int ret;
  3320. int err;
  3321. u64 qgroupid;
  3322. if (!capable(CAP_SYS_ADMIN))
  3323. return -EPERM;
  3324. ret = mnt_want_write_file(file);
  3325. if (ret)
  3326. return ret;
  3327. sa = memdup_user(arg, sizeof(*sa));
  3328. if (IS_ERR(sa)) {
  3329. ret = PTR_ERR(sa);
  3330. goto drop_write;
  3331. }
  3332. trans = btrfs_join_transaction(root);
  3333. if (IS_ERR(trans)) {
  3334. ret = PTR_ERR(trans);
  3335. goto out;
  3336. }
  3337. qgroupid = sa->qgroupid;
  3338. if (!qgroupid) {
  3339. /* take the current subvol as qgroup */
  3340. qgroupid = root->root_key.objectid;
  3341. }
  3342. /* FIXME: check if the IDs really exist */
  3343. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3344. err = btrfs_end_transaction(trans, root);
  3345. if (err && !ret)
  3346. ret = err;
  3347. out:
  3348. kfree(sa);
  3349. drop_write:
  3350. mnt_drop_write_file(file);
  3351. return ret;
  3352. }
  3353. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  3354. {
  3355. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3356. struct btrfs_ioctl_quota_rescan_args *qsa;
  3357. int ret;
  3358. if (!capable(CAP_SYS_ADMIN))
  3359. return -EPERM;
  3360. ret = mnt_want_write_file(file);
  3361. if (ret)
  3362. return ret;
  3363. qsa = memdup_user(arg, sizeof(*qsa));
  3364. if (IS_ERR(qsa)) {
  3365. ret = PTR_ERR(qsa);
  3366. goto drop_write;
  3367. }
  3368. if (qsa->flags) {
  3369. ret = -EINVAL;
  3370. goto out;
  3371. }
  3372. ret = btrfs_qgroup_rescan(root->fs_info);
  3373. out:
  3374. kfree(qsa);
  3375. drop_write:
  3376. mnt_drop_write_file(file);
  3377. return ret;
  3378. }
  3379. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  3380. {
  3381. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3382. struct btrfs_ioctl_quota_rescan_args *qsa;
  3383. int ret = 0;
  3384. if (!capable(CAP_SYS_ADMIN))
  3385. return -EPERM;
  3386. qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
  3387. if (!qsa)
  3388. return -ENOMEM;
  3389. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3390. qsa->flags = 1;
  3391. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  3392. }
  3393. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  3394. ret = -EFAULT;
  3395. kfree(qsa);
  3396. return ret;
  3397. }
  3398. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  3399. {
  3400. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3401. if (!capable(CAP_SYS_ADMIN))
  3402. return -EPERM;
  3403. return btrfs_qgroup_wait_for_completion(root->fs_info);
  3404. }
  3405. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3406. void __user *arg)
  3407. {
  3408. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3409. struct inode *inode = file_inode(file);
  3410. struct btrfs_root *root = BTRFS_I(inode)->root;
  3411. struct btrfs_root_item *root_item = &root->root_item;
  3412. struct btrfs_trans_handle *trans;
  3413. struct timespec ct = CURRENT_TIME;
  3414. int ret = 0;
  3415. ret = mnt_want_write_file(file);
  3416. if (ret < 0)
  3417. return ret;
  3418. down_write(&root->fs_info->subvol_sem);
  3419. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3420. ret = -EINVAL;
  3421. goto out;
  3422. }
  3423. if (btrfs_root_readonly(root)) {
  3424. ret = -EROFS;
  3425. goto out;
  3426. }
  3427. if (!inode_owner_or_capable(inode)) {
  3428. ret = -EACCES;
  3429. goto out;
  3430. }
  3431. sa = memdup_user(arg, sizeof(*sa));
  3432. if (IS_ERR(sa)) {
  3433. ret = PTR_ERR(sa);
  3434. sa = NULL;
  3435. goto out;
  3436. }
  3437. trans = btrfs_start_transaction(root, 1);
  3438. if (IS_ERR(trans)) {
  3439. ret = PTR_ERR(trans);
  3440. trans = NULL;
  3441. goto out;
  3442. }
  3443. sa->rtransid = trans->transid;
  3444. sa->rtime.sec = ct.tv_sec;
  3445. sa->rtime.nsec = ct.tv_nsec;
  3446. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3447. btrfs_set_root_stransid(root_item, sa->stransid);
  3448. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3449. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  3450. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  3451. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  3452. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  3453. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3454. &root->root_key, &root->root_item);
  3455. if (ret < 0) {
  3456. btrfs_end_transaction(trans, root);
  3457. trans = NULL;
  3458. goto out;
  3459. } else {
  3460. ret = btrfs_commit_transaction(trans, root);
  3461. if (ret < 0)
  3462. goto out;
  3463. }
  3464. ret = copy_to_user(arg, sa, sizeof(*sa));
  3465. if (ret)
  3466. ret = -EFAULT;
  3467. out:
  3468. kfree(sa);
  3469. up_write(&root->fs_info->subvol_sem);
  3470. mnt_drop_write_file(file);
  3471. return ret;
  3472. }
  3473. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  3474. {
  3475. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3476. size_t len;
  3477. int ret;
  3478. char label[BTRFS_LABEL_SIZE];
  3479. spin_lock(&root->fs_info->super_lock);
  3480. memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  3481. spin_unlock(&root->fs_info->super_lock);
  3482. len = strnlen(label, BTRFS_LABEL_SIZE);
  3483. if (len == BTRFS_LABEL_SIZE) {
  3484. pr_warn("btrfs: label is too long, return the first %zu bytes\n",
  3485. --len);
  3486. }
  3487. ret = copy_to_user(arg, label, len);
  3488. return ret ? -EFAULT : 0;
  3489. }
  3490. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  3491. {
  3492. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3493. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  3494. struct btrfs_trans_handle *trans;
  3495. char label[BTRFS_LABEL_SIZE];
  3496. int ret;
  3497. if (!capable(CAP_SYS_ADMIN))
  3498. return -EPERM;
  3499. if (copy_from_user(label, arg, sizeof(label)))
  3500. return -EFAULT;
  3501. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  3502. pr_err("btrfs: unable to set label with more than %d bytes\n",
  3503. BTRFS_LABEL_SIZE - 1);
  3504. return -EINVAL;
  3505. }
  3506. ret = mnt_want_write_file(file);
  3507. if (ret)
  3508. return ret;
  3509. trans = btrfs_start_transaction(root, 0);
  3510. if (IS_ERR(trans)) {
  3511. ret = PTR_ERR(trans);
  3512. goto out_unlock;
  3513. }
  3514. spin_lock(&root->fs_info->super_lock);
  3515. strcpy(super_block->label, label);
  3516. spin_unlock(&root->fs_info->super_lock);
  3517. ret = btrfs_end_transaction(trans, root);
  3518. out_unlock:
  3519. mnt_drop_write_file(file);
  3520. return ret;
  3521. }
  3522. long btrfs_ioctl(struct file *file, unsigned int
  3523. cmd, unsigned long arg)
  3524. {
  3525. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3526. void __user *argp = (void __user *)arg;
  3527. switch (cmd) {
  3528. case FS_IOC_GETFLAGS:
  3529. return btrfs_ioctl_getflags(file, argp);
  3530. case FS_IOC_SETFLAGS:
  3531. return btrfs_ioctl_setflags(file, argp);
  3532. case FS_IOC_GETVERSION:
  3533. return btrfs_ioctl_getversion(file, argp);
  3534. case FITRIM:
  3535. return btrfs_ioctl_fitrim(file, argp);
  3536. case BTRFS_IOC_SNAP_CREATE:
  3537. return btrfs_ioctl_snap_create(file, argp, 0);
  3538. case BTRFS_IOC_SNAP_CREATE_V2:
  3539. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  3540. case BTRFS_IOC_SUBVOL_CREATE:
  3541. return btrfs_ioctl_snap_create(file, argp, 1);
  3542. case BTRFS_IOC_SUBVOL_CREATE_V2:
  3543. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  3544. case BTRFS_IOC_SNAP_DESTROY:
  3545. return btrfs_ioctl_snap_destroy(file, argp);
  3546. case BTRFS_IOC_SUBVOL_GETFLAGS:
  3547. return btrfs_ioctl_subvol_getflags(file, argp);
  3548. case BTRFS_IOC_SUBVOL_SETFLAGS:
  3549. return btrfs_ioctl_subvol_setflags(file, argp);
  3550. case BTRFS_IOC_DEFAULT_SUBVOL:
  3551. return btrfs_ioctl_default_subvol(file, argp);
  3552. case BTRFS_IOC_DEFRAG:
  3553. return btrfs_ioctl_defrag(file, NULL);
  3554. case BTRFS_IOC_DEFRAG_RANGE:
  3555. return btrfs_ioctl_defrag(file, argp);
  3556. case BTRFS_IOC_RESIZE:
  3557. return btrfs_ioctl_resize(file, argp);
  3558. case BTRFS_IOC_ADD_DEV:
  3559. return btrfs_ioctl_add_dev(root, argp);
  3560. case BTRFS_IOC_RM_DEV:
  3561. return btrfs_ioctl_rm_dev(file, argp);
  3562. case BTRFS_IOC_FS_INFO:
  3563. return btrfs_ioctl_fs_info(root, argp);
  3564. case BTRFS_IOC_DEV_INFO:
  3565. return btrfs_ioctl_dev_info(root, argp);
  3566. case BTRFS_IOC_BALANCE:
  3567. return btrfs_ioctl_balance(file, NULL);
  3568. case BTRFS_IOC_CLONE:
  3569. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  3570. case BTRFS_IOC_CLONE_RANGE:
  3571. return btrfs_ioctl_clone_range(file, argp);
  3572. case BTRFS_IOC_TRANS_START:
  3573. return btrfs_ioctl_trans_start(file);
  3574. case BTRFS_IOC_TRANS_END:
  3575. return btrfs_ioctl_trans_end(file);
  3576. case BTRFS_IOC_TREE_SEARCH:
  3577. return btrfs_ioctl_tree_search(file, argp);
  3578. case BTRFS_IOC_INO_LOOKUP:
  3579. return btrfs_ioctl_ino_lookup(file, argp);
  3580. case BTRFS_IOC_INO_PATHS:
  3581. return btrfs_ioctl_ino_to_path(root, argp);
  3582. case BTRFS_IOC_LOGICAL_INO:
  3583. return btrfs_ioctl_logical_to_ino(root, argp);
  3584. case BTRFS_IOC_SPACE_INFO:
  3585. return btrfs_ioctl_space_info(root, argp);
  3586. case BTRFS_IOC_SYNC:
  3587. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  3588. return 0;
  3589. case BTRFS_IOC_START_SYNC:
  3590. return btrfs_ioctl_start_sync(root, argp);
  3591. case BTRFS_IOC_WAIT_SYNC:
  3592. return btrfs_ioctl_wait_sync(root, argp);
  3593. case BTRFS_IOC_SCRUB:
  3594. return btrfs_ioctl_scrub(file, argp);
  3595. case BTRFS_IOC_SCRUB_CANCEL:
  3596. return btrfs_ioctl_scrub_cancel(root, argp);
  3597. case BTRFS_IOC_SCRUB_PROGRESS:
  3598. return btrfs_ioctl_scrub_progress(root, argp);
  3599. case BTRFS_IOC_BALANCE_V2:
  3600. return btrfs_ioctl_balance(file, argp);
  3601. case BTRFS_IOC_BALANCE_CTL:
  3602. return btrfs_ioctl_balance_ctl(root, arg);
  3603. case BTRFS_IOC_BALANCE_PROGRESS:
  3604. return btrfs_ioctl_balance_progress(root, argp);
  3605. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  3606. return btrfs_ioctl_set_received_subvol(file, argp);
  3607. case BTRFS_IOC_SEND:
  3608. return btrfs_ioctl_send(file, argp);
  3609. case BTRFS_IOC_GET_DEV_STATS:
  3610. return btrfs_ioctl_get_dev_stats(root, argp);
  3611. case BTRFS_IOC_QUOTA_CTL:
  3612. return btrfs_ioctl_quota_ctl(file, argp);
  3613. case BTRFS_IOC_QGROUP_ASSIGN:
  3614. return btrfs_ioctl_qgroup_assign(file, argp);
  3615. case BTRFS_IOC_QGROUP_CREATE:
  3616. return btrfs_ioctl_qgroup_create(file, argp);
  3617. case BTRFS_IOC_QGROUP_LIMIT:
  3618. return btrfs_ioctl_qgroup_limit(file, argp);
  3619. case BTRFS_IOC_QUOTA_RESCAN:
  3620. return btrfs_ioctl_quota_rescan(file, argp);
  3621. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  3622. return btrfs_ioctl_quota_rescan_status(file, argp);
  3623. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  3624. return btrfs_ioctl_quota_rescan_wait(file, argp);
  3625. case BTRFS_IOC_DEV_REPLACE:
  3626. return btrfs_ioctl_dev_replace(root, argp);
  3627. case BTRFS_IOC_GET_FSLABEL:
  3628. return btrfs_ioctl_get_fslabel(file, argp);
  3629. case BTRFS_IOC_SET_FSLABEL:
  3630. return btrfs_ioctl_set_fslabel(file, argp);
  3631. }
  3632. return -ENOTTY;
  3633. }