ioctl.c 89 KB

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