ioctl.c 98 KB

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