ioctl.c 89 KB

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