ioctl.c 82 KB

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