ioctl.c 79 KB

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