ioctl.c 97 KB

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