ioctl.c 75 KB

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