ioctl.c 79 KB

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