ioctl.c 96 KB

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