ioctl.c 80 KB

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