ioctl.c 89 KB

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